php Wordpress插件输出og:image:width和og:image:height标签,因为Facebook第一次共享URL时需要它们。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php Wordpress插件输出og:image:width和og:image:height标签,因为Facebook第一次共享URL时需要它们。相关的知识,希望对你有一定的参考价值。

<?php
/**
 * The plugin bootstrap file
 *
 * This file is read by WordPress to generate the plugin information in the plugin
 * admin area. This file also includes all of the dependencies used by the plugin,
 * registers the activation and deactivation functions, and defines a function
 * that starts the plugin.
 *
 * @link              http://example.com
 * @since             0.0.1
 * @package           Wordpress_SEO_OG_Image_Fix
 *
 * @wordpress-plugin
 * Plugin Name:       Wordpress SEO - og:image Fix
 * Plugin URI:        http://briannadeleasa.com
 * Description:       Allows you to input information specific to the company/website such as a logo image, address, phone number and social network links.
 * Version:           1.0.0
 * Author:            Brianna Deleasa
 * Author URI:        http://briannadeleasa.com
 * License:           GPL-2.0+
 * License URI:       http://www.gnu.org/licenses/gpl-2.0.txt
 * Text Domain:       wordpress-seo-og-image-fix
 * Domain Path:       /languages
 */

// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
	die;
}


add_filter( 'wpseo_opengraph', 'wordpress_seo_og_image_fix' );
/**
 * Temporary fix
 * OpenGraph add a og:image:width and og:image:height for FB async load of og:image issues
 *
 * https://github.com/Yoast/wordpress-seo/issues/2151
 *  https://developers.facebook.com/docs/sharing/webmasters/optimizing#cachingimages
 */
function wordpress_seo_og_image_fix() {
	global $wpseo_og;

	// will get a array with images
	$opengraph_images = new WPSEO_OpenGraph_Image( $wpseo_og->options );

	foreach ( $opengraph_images->get_images() as $img ) {
		// this block of code will first convert url of image to local path
		// for faster process of image sizes later
		$upload_dir = wp_upload_dir();
		$img_src = str_replace($upload_dir['url'], $upload_dir['path'], $img);
		$size = getimagesize($img_src);

		// display of this tags with Yoast SEO plugin
		$wpseo_og->og_tag( 'og:image:width', $size[0] );
		$wpseo_og->og_tag( 'og:image:height', $size[1] );
	}

}

以上是关于php Wordpress插件输出og:image:width和og:image:height标签,因为Facebook第一次共享URL时需要它们。的主要内容,如果未能解决你的问题,请参考以下文章

php 简单的OG共享数据(Wordpress版本)

php OG Facebook for Wordpress与Yoast

PHP 使Snipplr WordPress插件输出有效的XHTML

Wordpress简单字段插件-显示图像

如何使用 php 在 WordPress 插件中回显 json 数据 [重复]

og:图像不适用于 wordpress 页面