文章目录
一,生成 API 密钥
先登陆https://www.bing.com/webmasters/,点右上角的“齿轮”图标。
展开之后点“API 访问”,再点“API 密钥”,创建一个,完成之后可以点右边的复制图标。
二,代码
加到你主题的functions.php里边即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
function bd_post_json($urls, $api, $httpheard ) { $ch = curl_init(); $options = array( CURLOPT_URL => $api, CURLOPT_POST => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_POSTFIELDS => $urls, CURLOPT_HTTPHEADER => $httpheard, ); curl_setopt_array($ch, $options); $result = curl_exec($ch); curl_close($ch); } function bd_bing_api( $post_ID, $post, $update ) { if ( wp_is_post_revision( $post_ID ) || wp_is_post_autosave( $post_ID ) ) { return; } if ( 'publish' === $post->post_status ) { if ( 'post' === $post->post_type ) { $urls = '{ "siteUrl":"'.get_bloginfo('url').'","url":"'.get_permalink( $post ).'" }'; $api = 'https://ssl.bing.com/webmaster/api.svc/json/SubmitUrl?apikey=呢度填你刚申请的API-KEY'; $httpheard = array('Content-Type: application/json', 'charset=utf-8'); bd_post_json( $urls, $api, $httpheard ); } } } add_action( 'save_post', 'bd_bing_api', 10, 3 ); |
谢谢大佬分享代码