Skip to content

Instantly share code, notes, and snippets.

@dmhendricks
Last active October 6, 2019 22:45
Show Gist options
  • Save dmhendricks/63e135c22db3857f1dc1e69c5a6cdc16 to your computer and use it in GitHub Desktop.
Save dmhendricks/63e135c22db3857f1dc1e69c5a6cdc16 to your computer and use it in GitHub Desktop.
A simple WordPress plugin that adds Open Graph author tags for Facebook
<?php
/**
* @wordpress-plugin
* Plugin Name: Open Graph Author Tags
* Description: Adds author Open Graph tags to posts for Facebook
* Version: 1.0.0
* Author: Daniel M. Hendricks
* Author URI: https://www.danhendricks.com
* Plugin URI: https://gist.github.com/dmhendricks/63e135c22db3857f1dc1e69c5a6cdc16/
* Requires at least: 4.7
* Requires PHP: 5.6
*/
namespace CloudVerve;
defined( 'ABSPATH' ) || die();
final class Open_Graph_Author_Tags {
private static $instance;
public static function init() {
if ( !isset( self::$instance ) && !( self::$instance instanceof Open_Graph_Author_Tags ) ) {
self::$instance = new Open_Graph_Author_Tags;
// Add author tags
add_action( 'wp_head', [ self::$instance, 'opengraph_author_tags' ] );
}
return self::$instance;
}
/*
* Add author tags to page head
*
* @since 1.0.0
*/
public static function opengraph_author_tags() {
if( !is_single() ) return;
printf( '<meta property="author" content="%s" />', esc_attr( get_the_author() ) );
if( $author_facebook_link = filter_var( get_the_author_meta( 'facebook' ), FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED ) ) {
printf( '<meta property="article:author" content="%s" />', esc_attr( $author_facebook_link ) );
}
}
}
Open_Graph_Author_Tags::init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment