Last active
June 5, 2020 18:07
-
-
Save wpmudev-sls/c16465a3ffcd7135f4d67c7fbad922b8 to your computer and use it in GitHub Desktop.
[SmartCrawl Pro] - Add custom OG meta tags
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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