How to Programmatically Modify Meta Title if using RankMath WordPress Plugin

Do you need a temp WordPress site? Test it now!

Sharing is caring!

Normally to modify the WordPress pages’ meta title you can use the following filter.
doc ref: https://developer.wordpress.org/reference/hooks/the_title/

add_filter( 'the_title', 'SOME_FUNCTION', 50, 2 );

the callback will receive $post_title, $post_id as arguments.

If you’re using RankMath for your SEO stuff you need to use this filter: rank_math/frontend/title to modify the meta title.

add_filter('rank_math/frontend/title', function ($title) {
    global $post; // you can access the current page/post
    $title .= ' Append More Info Here';
    return $title;
});

I didn’t have the time to fully investigate why that’s the case. Maybe RankMath removes the default hooks to ensure no other plugin is dealing with meta title and description fields to avoid issues.

Sharing is caring!

Do you need a temp WordPress site? Test it now!
This code shared in this post/site is provided "AS IS" without any warranties or guarantees of any kind. The authors and distributors of this code cannot be held responsible for any damage, data loss, or other issues that may arise from its use. It is strongly recommended that you test this code in a staging environment before deploying it to a live site. Always ensure you have proper backups and understand the implications of running this code on your WordPress installation. Use at your own risk.

Leave a Comment

Your email address will not be published. Required fields are marked *