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>
<meta charset="utf-8" />
<title>WP REST API V2 Create Post With Jquery AJAX</title>
    url: 'http://yourwebsite/wp/wp-json/wp/v2/posts/',
    method: 'POST',
    crossDomain: true,
        '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 );

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

WP REST API Get Posts By Category

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


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.

wp rest api v2

WP REST API Get Media Detail By ID

WordPress WP REST API V2 get media attachment by id with php curl tutorial and code example

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 Create Category PHP cURL

Wordpress Tutorial WP REST API V2 Create Category With PHP cURL with code example