skip to Main Content

I would like to use custom shortcodes in Yoast SEO Plugin, but I can’t bring it to work. I want to put custom time specifications in the meta title.

This is my shortcode:

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

This is how I want the title in the Yoast to look like: THIS IS MY EXAMPLE POST in the year [yyyy]

Any ideas how I can make my shortcode work in Yoast?

5

Answers


  1. If you’d like your post to show the date at the end of your SEO Title of Yoast try: %%date%% within the SEO Title field of the yoast plugin on that particular post. For just the year use %%currentyear%%.

    In the event you’d like to do this globally you can do this within the post titles & metas section of the plugin settings.

    Here is a full list of their varables

    Login or Signup to reply.
  2. For anyone looking to add shortcode support for Yoast SEO’s Meta Title, here’s how I handled it. This code adds full shortcode support for Meta Title and adds a custom shortcode [year] to output the current year. Add this code to functions.php:

    // Add shortcode support to Yoast Meta Title using 'wpseo_title' filter
    add_filter('wpseo_title', 'filter_wpseo_title');
    function filter_wpseo_title($title) {
      $title = do_shortcode($title);  
      return $title;
    }
    
    // Add [year] shortcode (outputs current date in YYYY format)
    add_shortcode('year', 'year_shortcode');
    function year_shortcode() {
      $year = date('Y');
      return $year;
    }
    
    Login or Signup to reply.
  3. We’ve created a WordPress plugin to do/run/show shortcodes for Yoast SEO. It is currently in version 2.1.1. Unfortunately we cannot upload it to the WordPress repository due to possible copyright infringement (which is not true). So you may download, use parts of our code and use install the whole plugin directly from here:
    https://denra.com/wordpress/plugins/do-shortcodes-for-yoast-seo/

    We will continue updating the plugin with new features when needed but you may need to keep the URL to the plugin page on our website because we cannot upload to WordPress.org

    Login or Signup to reply.
  4. Add this code in functions.php file and it will work:

    // For adding [year] shortcode in WordPress Posts
    
    add_shortcode( 'year', 'sc_year' );
    function sc_year(){
        return date( 'Y' );
    }
    
    add_filter( 'single_post_title', 'my_shortcode_title' );
    add_filter( 'the_title', 'my_shortcode_title' );
    function my_shortcode_title( $title ){
        return do_shortcode( $title );
    }
    
    Login or Signup to reply.
  5. add_filter( 'wpseo_title', 'do_shortcode' );
    add_filter( 'wpseo_metadesc', 'do_shortcode' );
    add_filter( 'the_title', 'do_shortcode' );
    
    add_shortcode( 'year' , 'current_year' );
    
    function current_year() {
        $year = date("Y");
        return "$year";
    }
    

    Now you can use [year] in a title to post current year.

    i am writing this code by considering you have yoast seo plugin

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search