Skip to content

Instantly share code, notes, and snippets.

@alexwcoleman
Created August 1, 2018 02:57
Show Gist options
  • Save alexwcoleman/363ae264e8b4a4178215b398b53db9a1 to your computer and use it in GitHub Desktop.
Save alexwcoleman/363ae264e8b4a4178215b398b53db9a1 to your computer and use it in GitHub Desktop.
(function($) {
let pages = [
'Home',
'About',
'Contact'
// whatever....
];
function addPages(pageArray){
// Create a page with the WordPress REST API
let data = {};
data.status = 'publish';
data.type = 'page';
pageArray.forEach(page => {
data.title = page;
data.content = `I am the ${page} content`; // just for sh!ts and giggles
$.ajax({
method: "POST",
url: restapi_details.rest_url + 'wp/v2/pages', // getting data from wp_localize_script
data: data,
beforeSend: function ( xhr ) {
xhr.setRequestHeader( 'X-WP-Nonce', restapi_details.nonce );
},
success : function( response ) {
// Save the page ID in case you need it for something
var myNewPageID = response.id;
}
});
});
}
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment