WordPress Implementation
Open Graph Tags in WordPress (Yoast, RankMath & Custom PHP)
WordPress sites can configure OG metadata via popular SEO plugins (Yoast, Rank Math) or lightweight custom PHP snippets in functions.php.
Implementation Best Practices
- ✓Always set Featured Images on WordPress posts to populate og:image automatically.
- ✓Disable duplicate OG tag generators when running multiple SEO plugins.
- ✓Clear caching plugins (WP Rocket, W3 Total Cache) after updating OG settings.
WordPress Code Example
// Add to theme functions.php (No Plugin Needed)
function add_custom_open_graph_tags() {
if (is_single()) {
global $post;
$title = get_the_title();
$desc = get_the_excerpt();
$url = get_permalink();
$img = get_the_post_thumbnail_url($post->ID, 'full');
echo '<meta property="og:title" content="' . esc_attr($title) . '" />' . "\n";
echo '<meta property="og:description" content="' . esc_attr($desc) . '" />' . "\n";
echo '<meta property="og:url" content="' . esc_url($url) . '" />' . "\n";
if ($img) {
echo '<meta property="og:image" content="' . esc_url($img) . '" />' . "\n";
}
echo '<meta name="twitter:card" content="summary_large_image" />' . "\n";
}
}
add_action('wp_head', 'add_custom_open_graph_tags', 2);