Skip to content

Instantly share code, notes, and snippets.

@bdeleasa
Created March 20, 2017 17:02
Show Gist options
  • Save bdeleasa/0099bc17eb9f528d0f46cd54304654ae to your computer and use it in GitHub Desktop.
Save bdeleasa/0099bc17eb9f528d0f46cd54304654ae to your computer and use it in GitHub Desktop.
Wordpress plugin that outputs og:image:width and og:image:height tags because Facebook needs them when sharing URLs for the first time.
<?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] );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment