Changing wp_title from inside my WordPress Plugin

nearly finished my wp plugin for an estate agent,
I spidered the site for 404’s etc, i noticed that my property details pages were spider’d in which all 45 page titles were : ( details | sitename ) (page titles are shown dynamicly from an ID being passed via querystring)

now I’ve got my nice urls fixed, urls look like this…

Read More
wpsite.com/details/20043/property+for+sale+in+this+area

In Which…

  • propid = 20043
  • propname = property+for+sale+in+this+area

both of these are querystring values which are used to re-write the urls.

'query_vars' => array('propid', 'propname'),
'rules' => 
array( '(.+?)/([^/]+)/([^/]+)/?$' => 'index.php?pagename=$matches[1]&propid=$matches[2]&propname=$matches[3]' )
);

Now when the property details page is loaded im trying to hook into the wordpress filter
wp_title but this isnt working the way i expected..

this is the code im using to generate the titles

function wp_myplugin_property_title()
{
    $wp_acquaint_id = get_option("wp_system_id");
    $propid = get_query_var('propid');
    if(isset($propid)){
        $seotitle = wp_myplugin_seo_title($propid);
    }else{
        $seotitle = "TEST Title";   
    }
    return $seotitle;   
}

if( is_page('details') ){
    add_filter('wp_title', wp_myplugin_property_title, 100);
}

the function used within that function: wp_myplugin_seo_title($propid) generates the actual title i want to use…

function wp_myplugin_seo_title($propid)
{
    $wp_acquaint_id = get_option("wp_acquaint_id");
    $xml = wp_myplugin_get_property($propid);
    foreach($xml->PropertiesDataSet->Properties as $node) {
        include('xml_loop.php');

        if($bedrooms==0){ }else{ $seo_title.= $bedrooms." bedroom "; }

        $seo_title.= wp_myplugin_get_property_type($type_id)." "; //ie:flat
        $seo_title.= str_replace("(","",$street);
        $seo_title.= " ".$town." | ".get_bloginfo('name');
    }
    return $seo_title;
}

Im finding that with the if(is_page()) in place around the filter the page title dosnt change and if i remove the is_page, the prop details page title works fine but!!!

while on the property listing page the page title is looping through all the properties on that page and producing a page title around 1000 char long..!

I have been hunting around for a better way to deal with this but any help would be great..

Cheers

Marty

ps: currently running wordpress seo by Yoast!
thats why ive set the priority as 100 in the add_filter just to see if it would overwrite the title..

Related posts

Leave a Reply

1 comment

  1. Using is_page in functions.php doesn’t work, as it runs before wp knows what page it’s going to render, or even if it’s a page to begin with. Stick the is_page() inside the function, and it should work. Like:

    function wp_myplugin_property_title()
    { 
      if( is_page('details') ){
        $wp_acquaint_id = get_option("wp_system_id");
        $propid = get_query_var('propid');
        if(isset($propid)){
            $seotitle = wp_myplugin_seo_title($propid);
        }else{
            $seotitle = "TEST Title";   
        }
        return $seotitle;   
      }
    }
    
    
    add_filter('wp_title', wp_myplugin_property_title, 100);