WordPress站点增加AMP移动端加速并主动推送到百度

首先,安装最新版谷歌原生态插件AMP,因为老版本会出现logo缺失和images缺失等问题。

AMP插件下载

可以在WordPress仪表盘,安装插件处直接搜索 WordPress 的 AMP 插件安装。或者在 WordPress 插件库下载上传.。

AMP插件使用

默认只需要文章,你可以根据自己需求勾选文章、页面、和媒体。
图片[1]-WordPress站点增加AMP移动端加速并主动推送到百度

将AMP页面提交到百度

在你的 functions.php 文件中加入以下代码

/**
 * WordPress增加谷歌AMP加速移动页面并自动推送到百度
 */
if(!function_exists('Baidu_amp')){
    function Baidu_amp($post_ID) {
        //已成功推送的文章不再推送
        if(get_post_meta($post_ID,'Baiduamp',true) == 1) return;
        $url = get_permalink($post_ID);
		if(get_post_type($post_ID)=='page'){
		$url=$url.'?amp';
		}
		if(get_post_type($post_ID)=='post'){
		$url=$url.'/amp/';
		}
        $api = 'http://data.zz.baidu.com/urls?site=***************&token=*************=amp';
        //登录百度搜索资源平台 >> 网站支持 >> 数据引入 >> MIP&AMP >> AMP 下方的数据提交就能看到 AMP 推送接口调用地址
        $request = new WP_Http;
        $result = $request->request( $api , array( 'method' => 'POST', 'body' => $url , 'headers' => 'Content-Type: text/plain') );
        $result = json_decode($result['body'],true);
        //如果推送成功则在文章新增自定义栏目 Baiduamp,值为 1
        if (array_key_exists('success_amp',$result)) {
            add_post_meta($post_ID, 'Baiduamp', 1, true);
        }
    }
    add_action('publish_post', 'Baidu_amp', 0);
}

就是这么简单。

© 版权声明
THE END
喜欢就支持一下吧
点赞6赞赏 分享