Created
August 1, 2018 02:57
-
-
Save alexwcoleman/363ae264e8b4a4178215b398b53db9a1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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