WP CUSTOM OG OVERRIDE
DATE: 2025-01-16 15:27:49
STATUS: PUBLISHED
AUTHOR: illphated

// Save custom meta data function custom_og_override_save_meta_data($post_id) { // Verify nonce if (!isset($_POST['custom_og_override_nonce']) || !wp_verify_nonce($_POST['custom_og_override_nonce'], 'custom_og_override_save')) { return; }
// Save OG title if (isset($_POST['custom_og_title'])) { update_post_meta($post_id, '_custom_og_title', sanitize_text_field($_POST['custom_og_title'])); }
// Save OG description if (isset($_POST['custom_og_description'])) { update_post_meta($post_id, '_custom_og_description', sanitize_textarea_field($_POST['custom_og_description'])); }
// Save Twitter image if (isset($_POST['custom_twitter_image'])) { update_post_meta($post_id, '_custom_twitter_image', esc_url_raw($_POST['custom_twitter_image'])); } } add_action('save_post', 'custom_og_override_save_meta_data');
// Filter Yoast SEO output for OG and Twitter metadata function custom_og_override_wpseo_metadata($presentation) { $post_id = get_the_ID(); if (!$post_id) return $presentation;
// Check for custom OG values $custom_og_title = get_post_meta($post_id, '_custom_og_title', true); $custom_og_description = get_post_meta($post_id, '_custom_og_description', true); $custom_twitter_image = get_post_meta($post_id, '_custom_twitter_image', true);
// Override OG title if (!empty($custom_og_title)) { $presentation->open_graph->title = $custom_og_title; }
// Override OG description if (!empty($custom_og_description)) { $presentation->open_graph->description = $custom_og_description; }
// Override Twitter image if (!empty($custom_twitter_image)) { $presentation->twitter->image = $custom_twitter_image; $presentation->open_graph->image = $custom_twitter_image; // Optional: Sync with OG image }
return $presentation; } add_filter('wpseo_frontend_presentation', 'custom_og_override_wpseo_metadata');