Hello,
Thank you for reaching out with your question about adding a shortcode to the title. To add a shortcode to your post title using Rank Math, you can follow these steps:
- Navigate to the Theme File Editor by going to Appearance → Theme File Editor in your WordPress admin area.
- Choose the
rank-math.php
file from the list of theme files shown on the right side of the screen.
- Copy and paste the following code snippet in the code editor:
// Create Year shortcode to replace with current year.
add_shortcode( 'year', 'current_year' );
function current_year() {
$year = date( 'Y' );
return $year;
}
// To allow Rank Math Frontend title to parse shortcode.
add_filter(
'rank_math/frontend/title',
function ( $title ) {
return do_shortcode( $title );
}
);
// Activate shortcode function in Post Title.
add_filter( 'the_title', 'do_shortcode' );
- Finally, click the Update File button at the bottom of the screen.
By following these steps, you can use the [year]
shortcode in your post title, and it will be replaced with the current year. If you have any further questions or need assistance, feel free to ask.
Sources:
Set the Current Year in Post Title Using the [year] Shortcode » Rank Math
How to Fix “Either ‘name’ or ‘item.name’ should be specified” Error » Rank Math
How to Easily Use PHP Code on Your Website » Rank Math
Homepage SEO Settings via Meta Title and Description
rank-math.php does not exist.
Hello,
Thank you for contacting Rank Math support.
You can follow this guide to create the rank-math.php file: https://rankmath.com/kb/wordpress-hooks-actions-filters/#rank-math-php-file
Let us know how that goes. Looking forward to helping you.
Hello,
We are not sure what challenge you are facing. Were you able to create the rank-math.php file within your active theme files? Ensure you added <?php
to the beginning of the file.
An alternative to using the rank-math.php file is to use the Code Snippets plugin.
https://rankmath.com/kb/wordpress-hooks-actions-filters/#num-1-1-install-code-snippets-plugin
Let us know how that goes. We look forward to helping you resolve this issue.
Thanks.
Hello,
Since we did not hear back from you for 15 days, we are assuming that you found the solution. We are closing this support ticket.
If you still need assistance or any other help, please feel free to open a new support ticket, and we will be more than happy to assist.
Thank you.