Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wpmudev-sls/c16465a3ffcd7135f4d67c7fbad922b8 to your computer and use it in GitHub Desktop.
Save wpmudev-sls/c16465a3ffcd7135f4d67c7fbad922b8 to your computer and use it in GitHub Desktop.
[SmartCrawl Pro] - Add custom OG meta tags
<?php
/**
* Plugin Name: [SmartCrawl Pro] - Add custom OG meta tags
* Plugin URI: https://premium.wpmudev.org/
* Description: Removing existing OG meta tags with custom ones (as of 2.6.1)
* Author: Alessandro Kaounas @ WPMUDEV
* Author URI: https://premium.wpmudev.org/
* Task: SLS-96
* License: GPLv2 or later
*/
add_action( 'template_redirect', function(){
// Return if not on home page.
if( ! is_home() || ! is_front_page() ){
return;
}
$latest_post = wp_get_recent_posts(
array(
'numberposts' => 1,
'post_status' => 'publish'
)
);
if( ! $latest_post ){
return;
}
if( ! class_exists( 'Smartcrawl_OpenGraph_Printer' ) ) {
return;
}
// Get SmartCrawl instance
$og_print = Smartcrawl_OpenGraph_Printer::get();
// Remove any existing OG meta tags
remove_action( 'wp_head', array( $og_print, 'dispatch_og_tags_injection' ), 50 );
remove_action( 'wds_head-after_output', array( $og_print, 'dispatch_og_tags_injection' ) );
// Add custom OG tags via 'print_og_tag' method
add_action( 'wp_head', function() use ( $og_print, $latest_post ){
// Add your own below
$og_print->print_og_tag( 'og:type', get_post_type( $latest_post ) );
$og_print->print_og_tag( 'og:title', get_the_title( $latest_post ) );
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment