Php Facebook Open Graph仍保留旧的元数据,而debugger显示最新的元数据
我在functions.php(wordpress)中添加了以下代码,以动态添加元数据,因为每个页面上的元数据都不同Php Facebook Open Graph仍保留旧的元数据,而debugger显示最新的元数据,php,wordpress,facebook,facebook-opengraph,Php,Wordpress,Facebook,Facebook Opengraph,我在functions.php(wordpress)中添加了以下代码,以动态添加元数据,因为每个页面上的元数据都不同 //facebook opengraph //Adding the Open Graph in the Language Attributes function add_opengraph_doctype( $output ) { return $output . ' xmlns:og="http://opengraphprotocol.org/schema/"
//facebook opengraph
//Adding the Open Graph in the Language Attributes
function add_opengraph_doctype( $output ) {
return $output . ' xmlns:og="http://opengraphprotocol.org/schema/" xmlns:fb="http://www.facebook.com/2008/fbml"';
}
add_filter('language_attributes', 'add_opengraph_doctype');
//Lets add Open Graph Meta Info
function insert_fb_in_head() {
global $post;
if ( !is_singular()) //if it is not a post or a page
return;
echo '<meta property="fb:app_id" content="???"/>';
echo '<meta property="og:title" content="' . get_the_title() . '"/>';
echo '<meta property="og:type" content="article"/>';
echo '<meta property="og:url" content="' . get_permalink() . '?fbrefresh=CAN_BE_ANYTHING"/>';
echo '<meta property="og:site_name" content="MY SITE"/>';
echo '<meta property="og:image:width" content="100"/>';
echo '<meta property="og:image:height" content="100"/>';
if(!has_post_thumbnail( $post->ID )) { //the post does not have featured image, use a default image
$default_image="www.mysite.com/wp-content/uploads/2017/04/Marine-1.jpg"; //replace this with a default image on your server or an image in your media library
echo '<meta property="og:image" content="' . $default_image . '"/>';
}
else{
$thumbnail_src = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'medium' );
echo '<meta property="og:image" content="' . esc_attr( $thumbnail_src[0] ) . '"/>';
}
echo "
";
}
add_action( 'wp_head', 'insert_fb_in_head', 5 );
//facebook opengraph
//在语言属性中添加开放图
函数add\u opengraph\u doctype($output){
返回$output。'xmlns:og='http://opengraphprotocol.org/schema/“xmlns:fb=”http://www.facebook.com/2008/fbml"';
}
add_filter('language_attributes','add_opengraph_doctype');
//让我们添加开放图元信息
函数插入头()中的函数{
全球$员额;
if(!is_singular())//如果它不是帖子或页面
返回;
回声';
回声';
回声';
回声';
回声';
回声';
回声';
如果(!has_post_缩略图($post->ID)){//文章没有特色图像,请使用默认图像
$default_image=“www.mysite.com/wp content/uploads/2017/04/Marine-1.jpg”//将其替换为服务器上的默认图像或媒体库中的图像
回声';
}
否则{
$thumbnail\u src=wp\u get\u attachment\u image\u src(get\u post\u thumbnail\u id($post->id),'medium');
回声';
}
回声“
";
}
添加行动('wp_head','insert_fb_in_head',5);
之前,我直接在标题中添加了开放图元数据,这对于测试来说不是动态的
现在,当我在调试器中创建新元数据时,它显示了正确的元数据。但是当我点击我的站点上的共享按钮时,它仍然显示不再存在的旧元数据
即使页面源显示正确/最新的元数据,但共享者仍然显示旧的、不存在的元数据
如何确保在我单击“共享”按钮的每个页面上提取新的元数据?您必须向FB api发送缓存清除请求。但是它响了
例如,通过卷曲(但它会持续很长时间)
也许可以通过javascript试试,你确定这不是缓存问题吗?请某人尝试在FB上共享您的URL。。。无需发布,只需粘贴您的博客链接,并查看预览中显示的打开的图形信息。@brasofilo,在fb上粘贴链接时,我会看到更新的元数据。即使调试器也会显示相同的内容,但只有sharer对话框会继续显示我已经从标题中删除的非常旧的元数据。此部分缓存在某个位置,似乎很难检测到。因为即使是共享按钮也被缓存了,所以它看起来是。我删除了共享按钮,但页面仍然显示它。我认为从另一个FB帐户和另一台计算机上检查它很重要。@brasofilo,用另一个帐户在另一台计算机上检查
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://graph.facebook.com/");
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, 'id='.$url.'&scrape=true'); //site url
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type: multipart/form-data'));
$curl_results = curl_exec($curl);
curl_close($curl);