0

We store the data for the user profiles on our site in a mongodb and provide them via a REST API. So i added a page template page-profile.php. In that file we call the REST API and fill the template with the responding data. We added rewrite_rules to the functions.php, so the profile template knows which profile fetch from the REST API.

functions.php

add_rewrite_rule('^termin/([^/]*)/([^/]*)/([^/]*)$',
                 'index.php?pagename=profile&location=$matches[1]&industry=$matches[2]&slug=$matches[3]',
                 'top'); 

page-profile.php

$slug = $location = htmlspecialchars(urldecode(get_query_var('slug')));
$office = $dataAccess->getProfile($slug);

That all works fine, we use the $slug to get the correct data from the REST API and store them in the $office variable. $office->name describe the name of the profile. My question is: How can i use the $office->name in the page title (needed for better SEO)?

I know I can use the the wordpress filter wp_title to set the title dynamically in the functions.php. But how I can use the $office->name (page-profile.php) in the wp_title filter (functions.php)?

  • When is $dataAccess initialized? – s_ha_dum May 25 '14 at 15:41
  • in functions.php – tschiela May 25 '14 at 15:47
0

I am guessing that $dataAccess is instantiated as a global. If that is true, this code...

$slug = $location = htmlspecialchars(urldecode(get_query_var('slug')));
$office = $dataAccess->getProfile($slug);

... should also work in a wp_title callback. You'd just need global $dataAccess; to pull the variable into scope, though there are probably more elegant ways to do it.

  • Sorry for the long time i didnt write something. The problem is, that the page-profile.php will be executed later than the functions.php. So i never can get the value, without calling the database twice with the $dataAccess object. – tschiela Jun 23 '14 at 19:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.