WordPress代码实现提交链接到熊掌号带后台面板。
熊掌号面板实现
WordPress后台面板的使用方法以及讲过了,可以参考这篇文章:为WordPress文章后台新增字段面板,这里就不多做说明,咱们直接进入正题。在你的主题functions.php文件中,新增如下代码:
//熊掌号面板配置
$postmeta_xzh = array(
array(
"title" => "原创文章",
"name" => "is_original",
"std" => ""
)
);
//熊掌号推送面板
function hui_postmeta_xzh() {
global $post, $postmeta_xzh;
foreach($postmeta_xzh as $meta_box) {
$meta_box_value = get_post_meta($post->ID, $meta_box['name'], true);
if($meta_box_value == "")
$meta_box_value = $meta_box['std'];
echo '<p><label><input '.($meta_box_value?'checked':'').' type="checkbox" value="1" name="'.$meta_box['name'].'"> '.(isset($meta_box['title']) ? $meta_box['title'] : '').'</label></p>';
}
$tui = get_post_meta($post->ID, 'xzh_tui_back', true);
if( $tui ) echo '<p>实时推送结果:'.$tui.'</p>';
echo '<input type="hidden" name="post_newmetaboxes_noncename" id="post_newmetaboxes_noncename" value="'.wp_create_nonce( plugin_basename(__FILE__) ).'" />';
}
//创建面板,其实是调用了上面的方法
function hui_postmeta_xzh_create() {
global $theme_name;
if ( function_exists('add_meta_box') ) {
add_meta_box( 'postmeta_xzh_boxes', __('百度熊掌号设置', 'haoui'), 'hui_postmeta_xzh', 'post', 'normal', 'high' );
}
}
//数据保存
function hui_postmeta_xzh_save( $post_id ) {
global $postmeta_xzh;
if ( !wp_verify_nonce( isset($_POST['post_newmetaboxes_noncename'])?$_POST['post_newmetaboxes_noncename']:'', plugin_basename(__FILE__) ))
return;
if ( !current_user_can( 'edit_posts', $post_id ))
return;
foreach($postmeta_xzh as $meta_box) {
$data = isset($_POST[$meta_box['name']]) ? $_POST[$meta_box['name']] : '';
if(get_post_meta($post_id, $meta_box['name']) == "")
add_post_meta($post_id, $meta_box['name'], $data, true);
elseif($data != get_post_meta($post_id, $meta_box['name'], true))
update_post_meta($post_id, $meta_box['name'], $data);
elseif($data == "")
delete_post_meta($post_id, $meta_box['name'], get_post_meta($post_id, $meta_box['name'], true));
}
}
//使用钩子创建面板
add_action('admin_menu', 'hui_postmeta_xzh_create');
//使用钩子,当文章保存时保存熊掌号配置数据
add_action('save_post', 'hui_postmeta_xzh_save');
到此我们在WordPress后台发布文章页面就可以看到一个关于熊掌号设置的面板,但没有实际功能,面板如下:

熊掌号提交代码
接着在刚刚的functions.php代码中添加如下代码:
// 熊掌号 新文章发布时实时推送
add_action('publish_post', 'tb_xzh_post_to_baidu');
//使用钩子在发布文章时执行下面的函数
function tb_xzh_post_to_baidu() {
$appid="111";//熊掌号appid
$token="222";//熊掌号token
global $post;
$plink = get_permalink($post->ID);
if( 'publish' !== $post->post_status && $plink ){
$isoriginal = get_post_meta($post->ID, 'is_original', true);
$urls = array();
$urls[] = $plink;
$api = 'http://data.zz.baidu.com/urls?appid='. $appid .'&token='.$token .'&type=realtime';
if( $isoriginal ){
$api .= ',original';
}
$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);
$result = json_decode($result);
$result_text = '成功';
if( $result->error ){
$result_text = '失败 '.$result->message;
}
update_post_meta($post->ID, 'xzh_tui_back', $result_text);
}
}
只需要把其中的熊掌号配置改为你自己的就好了,实际效果如下:
