Search code examples
wordpressreplacetitledynamically-generatedhead

Dynamically Replace <title> after Wordpress/Theme/Plugins generate <head>?


OK I have been doing lots of digging and trying to come up with the best solution and really would like some insight and help on this issue.

The problem: I have a Wordpress/Theme/Plugin combo set for my site that works great. Between WordPress, the Theme, and plugins such as SEO plugins, there is LOTS of things going on in the title generation for the site.

I have a template page that I want to be able to change the title on dynamically. I don't want to mess with the whole site, just this template page so it will display :dynamic item name +" Item Details" for the titles . I would like to possibly also replace the meta description as well, but I should be able to use the same method as the title so let’s focus on for now.

Example Page: http://www.ddmsrealm.com/dungeons-and-dragons-database/item-info/?ddoRareItemID=2507&ddoQuestID=352&ddoSeriesID=39 - This has a generic page title.

There is a function in the template page that gets and builds the header. After this is where I would probably need to put in code to replace the tag in the already generated .

So the question is how to change it?

Can you replace a generated tag with just PHP after the ? Could this be done with a function? This change is for SEO so I do not think a Javascript will work due to search engines not executing JavaScript. Would custom fields be the solution?

I want to affect just this template. I need it for SEO. And I don't think I can do this at the Theme or Plugin level since I just want to modify this one page, the rest generate just fine.

Thanks a ton for your help and insight.


Solution

  • Here is the final working code for this question. The above responses were a great help in getting this code very close. Since this was passing the information between pages/scripts a global variable was needed.

    $ddmsrealmitemname = $row_rsMainItemListing['Name'];
    
    add_filter( 'wp_title', 'my_ddmseo_wp_title', 100);
    function my_ddmseo_wp_title($title) {
        global $ddmsrealmitemname;
        $title = "{$ddmsrealmitemname} | Item Details | Dungeons and Dragons Online";
        return $title;
    }
    

    This script now scans the current items that has been selected in the recordset and passes that on over riding the SEO plugin title generation.

    This seemed to work well and was done for SEO reasons but it appears that Google is still not indexing these pages. Regardless, this is a nice little trick to overide default title generation on dynamic page items.