Last active
February 10, 2020 10:58
-
-
Save mebusw/9ff2ccc46e41acf213f07b6ffdcf7a2e 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
<?php get_header(); ?> | |
<?php header('Content-type: text/html; charset=utf-8'); ?> | |
<h1>WP API with nouces</h1> | |
<?php | |
$jarr=array( | |
'root' => esc_url_raw( rest_url() ), | |
'nonce' => wp_create_nonce( 'wp_rest' ) | |
); | |
//echo 'Now: '. date('Y-m-d H:i:s') ."\n"; | |
echo $str=json_encode($jarr).'\n'; | |
echo 'is_user_logged_in='.is_user_logged_in(); | |
?> | |
<input type="" name="" value="XP与SCRUM" placeholder="输入新文章标题" id="new-article-title"> | |
<div id="course-list"></div> | |
<script type="text/javascript"> | |
var wpApiSettings = {} | |
wpApiSettings.nonce = "<?php echo $jarr['nonce'] ?>" | |
console.log("WP nonce="+wpApiSettings.nonce) | |
$.ajax( { | |
url: "https://www.uperform.cn/wp-json/wp/v2/posts?per_page=15&categories=4&_fields=author,id,excerpt,title,link,categories,featured_media", | |
method: 'GET', | |
beforeSend: function ( xhr ) { | |
xhr.setRequestHeader( 'X-WP-Nonce', wpApiSettings.nonce ); | |
}, | |
data:{ } | |
} ).done( function ( response ) { | |
console.log( response ); | |
$('#course-list').html(JSON.stringify(response)) | |
} ); | |
$.ajax( { | |
url: "https://www.uperform.cn/wp-json/wp/v2/posts/3380", | |
method: 'POST', | |
beforeSend: function ( xhr ) { | |
xhr.setRequestHeader( 'X-WP-Nonce', wpApiSettings.nonce ); | |
}, | |
data:{ | |
'title' : '演讲实录丨真正提升软件团队能力的方法,唯有XP极限编程' + wpApiSettings.nonce+ '|' + new Date(), | |
} | |
} ).done( function ( response ) { | |
console.log( response ); | |
} ); | |
$.ajax( { | |
url: "https://www.uperform.cn/wp-json/wp/v2/posts", | |
method: 'POST', | |
beforeSend: function ( xhr ) { | |
xhr.setRequestHeader( 'X-WP-Nonce', wpApiSettings.nonce ); | |
}, | |
data:{ | |
'title' : $('#new-article-title').val() + wpApiSettings.nonce+ '|' + new Date(), | |
'categories': [4, 24] | |
} | |
} ).done( function ( response ) { | |
console.log( response ); | |
} ); | |
</script> | |
<?php get_footer(); ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment