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 Create Comment Into A Post

Wordpress Tuorial WP REST API Create Comment Into A Post.

wp rest api v2

WP REST API Get Posts By Category

WordPress REST API tutorial get posts by category id with php script

wp rest api v2

Disable WP Rest API V2

Disable Wordpres WP JSON – WP Rest API V2 with and without plugin

WP REST API V2 Auth

WP REST API Delete Post With PHP cURL

Wordpress tutorial WP REST API Delete Post With PHP cURL with code example

WP REST API V2 Auth

WP REST API Create User With PHP cURL

WP REST API Create User With PHP cURL. PHP code example to create user for your wordpress website.