/** * Register meta so WordPress stores, sanitises and exposes it in the * Block Editor sidebar (no extra JS required). */ foreach ( [ 'og_title', 'og_description', 'og_image' ] as $key ) { register_post_meta( '', $key, [ 'show_in_rest' => true, // Gutenberg UI 'single' => true, 'type' => 'string', 'auth_callback'=> '__return_true', // authors & up ] ); } /** * 2️⃣ Output one canonical set of OG / Twitter tags early in . */ add_action( 'wp_head', function () { if ( ! is_singular() ) { return; } $id = get_queried_object_id(); // author-editable values, with automatic fall-backs $title = get_post_meta( $id, 'og_title', true ) ?: single_post_title( '', false ); $desc = get_post_meta( $id, 'og_description', true )?: wp_strip_all_tags( get_the_excerpt() ); $img = get_post_meta( $id, 'og_image', true ) ?: ( has_post_thumbnail( $id ) ? get_the_post_thumbnail_url( $id, 'full' ) : '' ); printf( "\n\n". ''."\n". ''."\n". ''."\n". ( $img ? ''."\n" : '' ). ''."\n", esc_attr( $title ), esc_attr( $desc ) ); }, 5 ); // priority 5 → runs before most themes/plugins