Yoast SEO中的自定义短代码

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?

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

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;
}