只在新文章发布时提交一次。
最后边几行用来记录提交状态的日志TXT,相对路径到wordpress根目录,觉得正常之后可以将代码注释掉。
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 |
function bd_baidu_api( $new_status, $old_status, $post ) { if ( 'publish' === $new_status && 'publish' !== $old_status ) { if ( 'post' === $post->post_type ) { $urls = array(); array_push($urls, get_permalink( $post )); $api = '→→→→→→→→→→→→→→→→→→→→换成你自己的←←←←←←←←'; $ch = curl_init(); $options = array( CURLOPT_URL => $api, CURLOPT_POST => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_POSTFIELDS => implode("\n", $urls), CURLOPT_HTTPHEADER => array('Content-Type: text/plain'), ); curl_setopt_array($ch, $options); $result = curl_exec($ch); //echo $result; $file=dirname(dirname(dirname(dirname(__FILE__)))).'/bapilog.txt'; if (is_writable($file)) { file_put_contents($file, $urls[0]."\n", FILE_APPEND | LOCK_EX); file_put_contents($file, $result."\n", FILE_APPEND | LOCK_EX); } } } } add_action( 'transition_post_status', 'bd_baidu_api', 10, 3 ); |
真噶系小白~ 要自己先创建bapilog.txt,代码仲要摆喺functions.php里边最开头!摆最后会提示错误~~
你睇下PHP報乜錯?代碼我依家用緊的,實際用過冇錯先敢髮出來的。
放系后边会出错,系前边就唔会。。 可能系我主题噶问题,宜家正常使用~完美!