WP REST API Create Post with jQuery Ajax

By | May 31, 2017

WP REST API v2 tutorial create post with jquery ajax using basic authentication.

In the previous post you can read how to create a single post in a wordpress website in WP REST API with php curl. At this post I will give you another way how to create a post using jQuery AJAX with basic authentication. Don’t forget to install and activated WP Basic Auth Plugin to make sure this function run properly.

<!DOCTYPE html>
<html lang="en">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<head>
<meta charset="utf-8" />
<title>WP REST API V2 Create Post With Jquery AJAX</title>
</head>
<body>
<script>
jQuery.ajax({
    url: 'http://yourwebsite/wp/wp-json/wp/v2/posts/',
    method: 'POST',
    crossDomain: true,
    data:{
        'title'     : 'This is A Post Title',
        'content'     : 'This is A post Content'
    },    
    beforeSend: function ( xhr ) {
    xhr.setRequestHeader( 'Authorization', 'Basic ' + btoa("username:password") );
    },
    success: function( data, txtStatus, xhr ) {
        console.log( data );
        //console.log( xhr.status );
    }
});
</script>
</body>
</html>

You can see at your posts section in your dashboard is there any new post. This method is support for cross domain , I already tested this script to post in a website from a localhost.

Related Posts

WP REST API V2 Auth

WP REST API Delete Post PHP File Get Contents

WordPress WP REST API Delete Post Tutorial With PHP File Get Contents Function

wp rest api v2

WP REST API Get Post By Id With PHP

WordPress WP REST API V2 Tutorial get post by post id using php script.

wp rest api v2

WP REST API Get Featured Image With Custom Endpoint

WP REST API Get Featured Image By Adding Custom Endpoint in Theme Function

wp rest api v2

WP REST API Get Post By Slug With PHP

WordPress WP REST API Tutorial Get Post Data By Slug With PHP

WP REST API V2 Auth

WP REST API Basic Authentication PHP File Get Contents

Wordpres Tutorial WP REST API Basic Authentication PHP File Get Contents. At previous post I already posted about wp rest api basic authentication with php cURL, but in some cases a hosting / server doesn’t has cURL module ( not installer , not enabled ).