wordpressshortcodeyoast

Wordpress shortcode not rendering in Yoast og:title


I have a simple shortcode which i use on post titles:

add_shortcode('year', 'year_shortcode');
function year_shortcode() {
    $year = date('Y');
    return $year;
}

add_filter( 'single_post_title', 'my_shortcode_title' );
add_filter( 'the_title', 'my_shortcode_title' );
add_filter('wpseo_title', 'my_shortcode_title');
add_filter( 'wpseo_metadesc', 'my_shortcode_title' );

function my_shortcode_title( $title ){
    return do_shortcode( $title );
}

It works fine but I suddenly realized that Yoast creates an og:title and it doesn't render there (meaning it doesn't render on Facebook or whatsapp)

I searched for an answer and couldn't find anythinng. Has anyone faced this before?

Maybe I need to run the og:title through a filter of some sort which is also fine, the question is how.

Thanks in advance.


Solution

  • I used this and it mostly works. It will update the og:title, and socials etc in Yoast.

    It does not update Yoast's default Schema output. This would probably need to be addressed by them.

    // Create Year shortcode to replace with current year.
    add_shortcode( 'currentyear', 'current_year' );
    function current_year() {
        $year = date( 'Y' );
        return $year;
    }
    // Activate shortcode function in Post Title.
    add_filter( 'the_title', 'do_shortcode' );
    add_filter( 'single_post_title', 'do_shortcode' );
    add_filter( 'wpseo_title', 'do_shortcode' );
    add_filter( 'wpseo_metadesc', 'do_shortcode' );
    add_filter( 'wpseo_opengraph_title', 'do_shortcode' );
    add_filter( 'wpseo_opengraph_desc', 'do_shortcode' );
    add_filter( 'wpseo_opengraph_site_name', 'do_shortcode' );
    add_filter( 'wpseo_twitter_title', 'do_shortcode' );
    add_filter( 'wpseo_twitter_description', 'do_shortcode' );
    add_filter( 'the_excerpt', 'do_shortcode' );