I currently have a shortcode which dynamically swaps out locations on a page to make each page specific to a location.

For example the url


Would make the H1 of my website be

Special Offers Scotland

But now I also want to change the title tag to allow effect search engine optimisation. I am unsure if it's possible to do this within my shortcode as it will execute within the loop and therefore the header.php has already finished loading.

Any guidance would be greatly appreciated.

  • 1
    Don't do this. Better build a custom meta box that allows the author to change the post's page title Jul 19, 2011 at 13:51
  • Unfortunately, this is the way it has to be done! There will only ever be 1 variable throughout the associated pages and that is the location. Jul 19, 2011 at 13:54

1 Answer 1


You are correct in your thinking that because the loop evaluates after the header you will not be able to get the contents of the loop there.

The easiest way to do this is probably with jQuery. Something like this:

<script type="text/javascript">
    $(document).ready(function() {
        document.title = 'blah';

Place this code in your loop, and the page title will be changed on after the document is ready. This may not be a desirable solution however, because some search engines do not evaluate JavaScript. You can use Google Webmaster Tools to see what the page looks like to the Google Crawler.

As an alternate solution, I believe All in One SEO Pack has the functionality that you describe:

  • You can override any title and set any META description and any META keywords you want.
  • Support for Custom Post Types
  • Advanced Canonical URLs
  • Fine tune Page Navigational Links
  • Built-in API so other plugins/themes can access and extend functionality
  • Support for CMS-style WordPress installations
  • Automatically optimizes your titles for search engines
  • Generates META tags automatically
  • Avoids the typical duplicate content found on WordPress blogs
  • For beginners, you don't even have to look at the options, it works out-of-the-box. Just install.
  • For advanced users, you can fine-tune everything
  • I have ruled out javascript as this site needs to be 100% crawler friendly as it is a marketing project. In regards to AIOSEOP, I don't have physical wordpress pages so I can't change these titles. Jul 19, 2011 at 13:34
  • There is no meaning of changing title using JavaScript. Sep 21, 2018 at 16:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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