WordPress REST API Tutorial (Real Examples)
WordPress REST API Tutorial (Real Examples)
Let’s learn about the new WordPress REST API.
Link to my website:
My HTML & CSS Course:
My “Get a Developer Job” course:
Starter AJAX Code:
Starter Form HTML & CSS:
Link to download zip of finished theme files:
Add me on Twitter for webDev resources and cat pics:
My code not working .. please help me anyone
(function( $ ) {
“use strict”;
var quickAddButton = document.querySelector(“#quick-add-button”);
if(quickAddButton){
quickAddButton.addEventListener(“click”, function(){
var ourPostData = {
“title”: document.querySelector(‘.admin-quick-add [name=”title”]’).value,
“content”: document.querySelector(‘.admin-quick-add [name=”content”]’).value,
“status”: “publish”
};
var createPost = new XMLHttpRequest();
var magicalData=”;
createPost.open(“POST”, “http://localhost/wordpress/wp-json/wp/v2/posts”);
createPost.setRequestHeader(“X-WP-Nonce”, magicalData.nonce);
createPost.setRequestHeader(“Content-type”, “application/json;charset=UTF-8″);
createPost.send(JSON.stringify(ourPostData));
createPost.onreadystatechange = function(){
if(createPost.readyState == 4){
if(createPost.status == 201){
document.querySelector(‘.admin-quick-add [name=”title”]’).value=”;
document.querySelector(‘.admin-quick-add [name=”content”]’).value=”;
}else{
alert(“Error- Try Again”);
}
}
};
});
};
})( jQuery );
//function.php code
wp_enqueue_script(‘cus-main’, get_theme_file_uri( ‘/assets/js/cus-main.js’ ), array( ‘jquery’ ), ‘1.0’, true );
wp_localize_script( ‘cus-main’, ‘magicalData’, array(
‘nonce’ => wp_create_nonce(‘wp_reset’)
));
//Html code
Quick Add post
Create post
What would be a better tutorial is how to sign in the user from maybe something like a cordova app? otherwise the wp api is useless
hello nice tutorial,
my question is “how to retrieve more then 10 posts??”
i am not getting wp-json folder. Showing incorrect path. any idea?
Congrats Sir! Crossed 100k subscriber’s just now? Hope to see you making new tutorials soon 🙂
thanks, that’s one of the best tutorials I’ve watched!
You have a gift. I’ve been designing for about 5 years but recently started leaerning the JS stuff and I read lots of tutorials and also watch lots of videos but your videos stand out. You provide just the right amount of information and explain things in a very clear succinct way but are very thorough. Hard to explain, please keep it up.
Impressive tutorial Brad! Thanks !
thanks for replying
i would like to know all parameters like per_page, category etc,..
than can used with url
Thank you sir for your helpful advice
I love your teaching style. Perfect!!! Thanx for posting these vids!
Brad, do you have or will you do ANY tutorials on PHP? I bought your course on Web Dev with NodeJS, but it seems that there are countless jobs with PHP and not near as many openings with NodeJS? Thoughts?