Add Full Shortcode Support to Yoast SEO Meta Title and Meta Description with PHP code
By adding the following code snippets to the functions.php file, you can provide full shortcode support for Meta Title and Meta Description in the Yoast SEO plugin for your WordPress post title. This opens up a wide range of possibilities to customise your titles and use shortcodes to add dynamic elements.
Here is the example of the code snippet that adds full shortcode support for Meta Title and Meta Description in the Yoast SEO plugin. You need to add this piece of code to your functions.php file in your WordPress theme:
// Add shortcode support to Yoast SEO meta tags add_filter('wpseo_title', 'do_shortcode'); add_filter('wpseo_metadesc', 'do_shortcode');
Once you've added this piece of code, you can use shortcodes in your Meta Title and Meta Description fields in the Yoast SEO settings. For example, you can create a [year] shortcode to display the current year in the Meta Title or Meta Description:
Meta Title: "Welcome to our website [year]"
Meta Description: "Get the best deals in [year] on our website"
When the page appears in the search results, the shortcode [year] will be replaced with the current year.
Use shortcode in post title
If you want to extend the shortcode support functionality in your post title, simply add the following code snippet to your functions.php file. We use the add_filter function to add our customised function as a filter hook for the_title, which means it will be applied to the post titles.
add_filter( 'the_title', 'do_shortcode' );
Once you have added the code to the functions.php file, save the changes and update the file. Remember to always make a backup of your functions.php file before making any changes to it. And make sure to test your changes to ensure they work as expected.