Dynamic Titles for WordPress Static Pages

EDIT: The links in this post no longer work; I have taken down the Secret of the Solstice repository. The plugin for the repository can be downloaded through this page.

I encountered a problem after pushing the Secret of the Solstice repository live. As you click through the pages to the various items, the browser title remained the same throughout.

If someone were to search for the Anti-Death Penalty Pendant 1% on a search engine, the only listings which would appear would be the main SotS page if by chance the item was listed under the latest gears column or the main gears page. The actual item page would not appear due to the browser title being the same as the main gears page.

A little background info on how I set up the pages.

The front page for SotS is set up as the main page which I titled accordingly. Then there are six sub-pages named after the different sections I wanted to cover–consumable items, gear items, other items, quests, npcs, and areas. I titled each of the six sub-pages with general titles.

Each sub-page uses a different template geared towards its content. The templates queries data from the database based on the identification number retrieved from the URL. So, the content is ever-changing, but I am using six pages to hold the changing content.

When a title is assigned to a page, WordPress treats the page as a static page even if the content is dynamic. So, that same title will be used each time.

My first thought to solve this problem was to change the header information. In the template, there is a get_header() call which includes the title, meta, script, and style information into the page. Instead of calling the function, I could manually edit each template to use a dynamic title.

However, there was a problem with this solution. I currently use the All in One SEO plugin to handle the title and meta information. During the WordPress loop, the plugin grabs and replaces the title with the user-specified title. So, when I manually changed the header information in the template, the plugin would swap out my changes for the one I entered for the page through WordPress.

I could deactivate the SEO plugin, but then that would affect the rest of the site. So, the next solution which I am currently using is to apply a check in the SEO plugin.

First, I installed the plugin is_child so I can check if the current page is a direct descendant of the main Secret of the Solstice page.

In the function rewrite_title() in all_in_one_seo_pack.php after this part of the code:

} else if (is_page()) {
    // we're not in the loop :(
    $authordata = get_userdata($post->post_author);
    if ($this->is_static_front_page()) {
        if ($this->internationalize(get_option('aiosp_home_title'))) {
            $header = $this->replace_title($header, $this->internationalize(get_option('aiosp_home_title')));
        }
    } else {
        $title = $this->internationalize(get_post_meta($post->ID, "title", true));
        if (!$title) {
            $title = $this->internationalize(wp_title('', false));
        }

I added:

if (is_child('secret-of-the-solstice')) {
    $new_title = $title . sots_subpage_title() . ' | ' . $this->internationalize(get_bloginfo('name'));;
}

I also moved this bit of code into an else encapsulation extending from the previous bit like so:

else {
    $title_format = get_option('aiosp_page_title_format');
    $new_title = str_replace('%blog_title%', $this->internationalize(get_bloginfo('name')), $title_format);
    $new_title = str_replace('%blog_description%', $this->internationalize(get_bloginfo('description')), $new_title);
    $new_title = str_replace('%page_title%', $title, $new_title);
    $new_title = str_replace('%page_author_login%', $authordata->user_login, $new_title);
    $new_title = str_replace('%page_author_nicename%', $authordata->user_nicename, $new_title);
    $new_title = str_replace('%page_author_firstname%', ucwords($authordata->first_name), $new_title);
    $new_title = str_replace('%page_author_lastname%', ucwords($authordata->last_name), $new_title);
}

Once the function determines the current page is a WordPress page and not the front page, there will be a check to determine if it is a child page of secret-of-the-solstice. The $new_title makes a call to a function which returns the dynamic part of the title. And, there we have it–dynamic titles.

3 thoughts on “Dynamic Titles for WordPress Static Pages

  1. Pingback: php folk: I need your help! (if you can)

  2. Pingback: php folk: I need your help! (if you can)

Leave a Reply

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