Skip to content

Instantly share code, notes, and snippets.

@phlbnks
Created August 2, 2019 11:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save phlbnks/b08e353071baad30ab88a41e0c6ffc9b to your computer and use it in GitHub Desktop.
Save phlbnks/b08e353071baad30ab88a41e0c6ffc9b to your computer and use it in GitHub Desktop.
Basic social OG tag generation for WordPress
<?php
/**
* Plugin Name: Social Test
*
* TODO: Output <meta name="twitter:site" content="@username" />? Needs custom option to read from.
* TODO: Custom option for social fallback image instead of core image?
*/
/**
* Adding the Open Graph schema definitions in the Language Attributes.
*
* @param string $lang_attributes Language attributes set to output in head.
* @return string Modified language attributes to output in head.
*/
function hd_add_opengraph_doctype( $lang_attributes ) {
// Add schema definitions of they aren't present already.
if ( false === stripos( $lang_attributes, 'prefix=' ) ) {
$lang_attributes .= ' prefix="og: http://ogp.me/ns# fb: http://ogp.me/ns/fb#"';
}
return $lang_attributes;
}
add_filter( 'language_attributes', 'hd_add_opengraph_doctype' );
/**
* Add Open Graph/meta tags to head.
*/
function hd_add_opengraph_tags() {
/**
* Setup.
*/
// Bail if Yoast or AIOSEO is installed, since they will handle things.
if ( class_exists( 'WPSEO_Options' ) || class_exists( 'All_in_One_SEO_Pack' ) ) {
return;
}
// Set a post global on single posts. This avoids grabbing content from the first post on an archive page.
if ( is_singular() ) {
global $post;
}
/**
* Build the default values.
*/
$post_content = ! empty( $post ) ? $post->post_content : ''; // Get the post content.
$default_content = ( $post_content ) ? wp_strip_all_tags( strip_shortcodes( $post_content ) ) : $post_content; // Strip all tags from the post content we just grabbed.
$default_title = get_bloginfo( 'name' ); // Set our default title.
$default_url = get_permalink(); // Set our default URL.
$default_base_description = ( get_bloginfo( 'description' ) ) ? get_bloginfo( 'description' ) : esc_html__( 'Visit our website to learn more.', '_s' ); // Set our base description.
$default_type = 'article'; // Set the card type.
// Get our custom logo URL. We'll use this on archives and when no featured image is found.
$logo_id = get_theme_mod( 'custom_logo' );
$logo_image = ( $logo_id ) ? wp_get_attachment_image_src( $logo_id, 'full' ) : '';
$logo_url = ( $logo_id ) ? $logo_image[0] : '';
// Set our final defaults.
$card_locale = get_locale();
$card_title = $default_title;
$card_description = $default_base_description;
$card_long_description = $default_base_description;
$card_url = $default_url;
$card_image = $logo_url;
$card_type = $default_type;
/**
* Start overrides.
*/
// All singles.
if ( is_singular() ) {
if ( has_post_thumbnail() ) {
$card_image = get_the_post_thumbnail_url();
}
}
// Single posts/pages that aren't the front page.
if ( is_singular() && ! is_front_page() ) {
$card_title = get_the_title() . ' - ' . $default_title;
$card_description = ( $default_content ) ? wp_trim_words( $default_content, 53, '...' ) : $default_base_description;
$card_long_description = ( $default_content ) ? wp_trim_words( $default_content, 140, '...' ) : $default_base_description;
}
// Categories, Tags, and Custom Taxonomies.
if ( is_category() || is_tag() || is_tax() ) {
$term_name = single_term_title( '', false );
$card_title = $term_name . ' - ' . $default_title;
$specify = ( is_category() ) ? esc_html__( 'categorized in', '_s' ) : esc_html__( 'tagged with', '_s' );
$queried_object = get_queried_object();
$card_url = get_term_link( $queried_object );
$card_type = 'website';
// Translators: get the term name.
$card_long_description = $card_description = sprintf( esc_html__( 'Posts %1$s %2$s.', '_s' ), $specify, $term_name );
}
// Search results.
if ( is_search() ) {
$search_term = get_search_query();
$card_title = $search_term . ' - ' . $default_title;
$card_url = get_search_link( $search_term );
$card_type = 'website';
// Translators: get the search term.
$card_long_description = $card_description = sprintf( esc_html__( 'Search results for %s.', '_s' ), $search_term );
}
if ( is_home() ) {
$posts_page = get_option( 'page_for_posts' );
$card_title = get_the_title( $posts_page ) . ' - ' . $default_title;
$card_url = get_permalink( $posts_page );
$card_type = 'website';
}
// Front page.
if ( is_front_page() ) {
$front_page = get_option( 'page_on_front' );
$card_title = ( $front_page ) ? get_the_title( $front_page ) . ' - ' . $default_title : $default_title;
$card_url = get_home_url();
$card_type = 'website';
}
// Post type archives.
if ( is_post_type_archive() ) {
$post_type_name = get_post_type();
$card_title = $post_type_name . ' - ' . $default_title;
$card_url = get_post_type_archive_link( $post_type_name );
$card_type = 'website';
}
// Media page.
if ( is_attachment() ) {
$attachment_id = get_the_ID();
$card_image = ( wp_attachment_is_image( $attachment_id ) ) ? wp_get_attachment_image_url( $attachment_id, 'full' ) : $card_image;
}
?>
<meta property="og:title" content="<?php echo esc_attr( $card_title ); ?>" />
<meta property="og:type" content="<?php echo esc_attr( $card_type ); ?>" />
<meta property="og:url" content="<?php echo esc_url( $card_url ); ?>" />
<meta property="og:image" content="<?php echo esc_url( $card_image ); ?>" />
<meta property="og:site_name" content="<?php echo esc_attr( $default_title ); ?>" />
<meta property="og:description" content="<?php echo esc_attr( $card_description ); ?>" />
<meta name="description" content="<?php echo esc_attr( $card_long_description ); ?>" />
<meta property="og:locale" content="<?php echo esc_attr( $card_locale ); ?>" />
<?php
}
add_action( 'wp_head', 'hd_add_opengraph_tags' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment