Modifying page titles with a WordPress plugin without altering theme code

Hey everyone,

I’m working on a WordPress site and I need some help. I want to change the title tags for certain pages, but I don’t want to mess with the theme files. Is there a way to do this using a plugin instead?

My theme uses the add_theme_support('title-tag') function, so I can’t use the old wp_title method anymore. I’ve heard that’s not recommended these days anyway.

I’m thinking there must be a way to override the title tag through a plugin, but I’m not sure how to go about it. Has anyone done this before? Any tips or code examples would be super helpful!

Here’s a basic outline of what I’m trying to do:

function custom_page_title($title) {
    if (is_page('about')) {
        return 'Our Amazing Story | ' . get_bloginfo('name');
    }
    return $title;
}
add_filter('document_title_parts', 'custom_page_title');

Does this look like it’s on the right track? Thanks in advance for any advice!

Your approach using the ‘document_title_parts’ filter is spot-on, Luke. It’s a clean way to modify titles without altering theme files. Here’s a slightly expanded version that might be more flexible:

function custom_page_titles($title_parts) {
    if (is_page('about')) {
        $title_parts['title'] = 'Our Amazing Story';
    } elseif (is_single()) {
        $title_parts['site'] = 'Read More at ' . get_bloginfo('name');
    }
    return $title_parts;
}
add_filter('document_title_parts', 'custom_page_titles');

This allows you to modify different parts of the title structure separately. Remember to add this to your site-specific plugin or a custom functionality plugin to keep it separate from your theme.

I’ve been in a similar situation, Luke, and I discovered that the ‘All in One SEO’ plugin can be a very efficient alternative. In my experience, after installing and activating the plugin, you simply navigate to the specific page you wish to modify, scroll down to the dedicated meta box, and input your custom title in the provided title field. This approach effectively overrides the default title without altering your theme files, allowing for centralized title management across multiple pages. If you prefer a code-based solution, your document_title_parts filter method is reliable—just ensure thorough testing on various page layouts.

hey luke, i’ve used the Yoast SEO plugin for this before. it lets u customize page titles without touching theme files. just install it, go to the page editor, and look for the ‘SEO title’ field. easy peasy! might be overkill if u only need title changes tho. ur code looks good too if u wanna go that route