I'm building a site that uses a custom template to display a set of listings that are pulled from an external API. I need to set the title and meta description values using data from the API.

I've found plenty of examples using functions.php and seo hooks but I need to avoid making a second call to the api when functions.php is called and would prefer to set the title and meta description during my initial api call.

  • On what hook is the API called? Please show your code. Otherwise, this question is too broad. – Dave Romsey Nov 9 '16 at 19:40
  • I was able to solve the problem by passing a global variable before get_header() was called in my template. I used the following post: wordpress.stackexchange.com/questions/149333/… – user135498 Nov 9 '16 at 19:51
  • Glad to hear! Please post your solution as an answer to this question, then come back in a couple of days and accept it so that it's closed out. – Dave Romsey Nov 9 '16 at 19:57
  • Also had to get title from API and only think that worked for me was getting the head and replacing from it - see here: wordpress.stackexchange.com/questions/269862/… – Fanky May 22 '19 at 16:16

Use global variables to pass values from the template to the header.

// in the template file  

global $my_page_title;  
$my_page_title = 'field value';  

global $my_meta_description;  
$my_meta_description = 'field value';  


// in the header file  
global $my_page_title;  
global $my_meta_description;  

echo $my_page_title;  
echo $my_meta_description;  

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.