WordPress – Modify Title value before Header is rendered

I have a page in WordPress which displays different content depending on the value of a query string see below

http://www.my-site.com/my-page/?id=123

My problem is that this the </code> and <code></p> <h1></code> tags get set to the value for <code>my-page</code> and not to the value associated to the content for <code>id=123</code></p> <p>I’ve wrote some javascript to dynamically update these elements but there is a 1-2 second lag, plus if I generate an xml file, the outputted file picks up the original <code>my-page</code> title.</p> <p>I’ve also explored the idea of modifying the WordPress function <code>wp_title()</code> but I’m pretty sure that doing this will eventually be overwrote when I upgrade WordPress.</p> <p>What I need really is a way to somehow dynamically set the page title just after it is retrieved but before the function <code>wp_title()</code> is called, any ideas would be great.</p> <div class="ad468"> <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <!-- 468x15wp --> <ins class="adsbygoogle" style="display:inline-block;width:468px;height:15px" data-ad-client="ca-pub-9166932973222694" data-ad-slot="1391582397"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> <p>Read more here: <a href="http://stackoverflow.com/questions/43913294/wordpress-modify-title-value-before-header-is-rendered" target="_blank" title="WordPress - Modify Title value before Header is rendered" rel="nofollow">WordPress – Modify Title value before Header is rendered</a></p> <div class='yarpp-related yarpp-related-none'> <p>No related posts.</p> </div> </div><!-- .entry-content --> </div> </div> </article><!-- #post-## --> <nav class="navigation post-navigation" role="navigation"> <h2 class="screen-reader-text">Post navigation</h2> <div class="nav-links"><div class="nav-previous"><a href="http://wordpressrelated.com/woocommerce-3-0-loads-all-images-increased-page-size-load-time/" rel="prev">Woocommerce 3.0+ loads all images -> increased page size / load time</a></div><div class="nav-next"><a href="http://wordpressrelated.com/wordpress-doesnt-find-my-custom-font/" rel="next">WordPress doesn’t find my custom font</a></div></div> </nav> <div id="comments" class="comments-area"> <div id="respond" class="comment-respond"> <h3 id="reply-title" class="comment-reply-title">Leave a Reply <small><a rel="nofollow" id="cancel-comment-reply-link" href="/wordpress-modify-title-value-before-header-is-rendered/#respond" style="display:none;">Cancel reply</a></small></h3> <form action="http://wordpressrelated.com/wp-comments-post.php" method="post" id="commentform" class="comment-form" novalidate> <p class="comment-notes"><span id="email-notes">Your email address will not be published.</span> Required fields are marked <span class="required">*</span></p><p class="comment-form-comment"><label for="comment">Comment</label> <textarea id="comment" name="comment" cols="45" rows="8" maxlength="65525" required="required"></textarea></p><p class="comment-form-author"><label for="author">Name <span class="required">*</span></label> <input id="author" name="author" type="text" value="" size="30" maxlength="245" required='required' /></p> <p class="comment-form-email"><label for="email">Email <span class="required">*</span></label> <input id="email" name="email" type="email" value="" size="30" maxlength="100" aria-describedby="email-notes" required='required' /></p> <p class="comment-form-url"><label for="url">Website</label> <input id="url" name="url" type="url" value="" size="30" maxlength="200" /></p> <p class="form-submit"><input name="submit" type="submit" id="submit" class="submit" value="Post Comment" /> <input type='hidden' name='comment_post_ID' value='145776' id='comment_post_ID' /> <input type='hidden' name='comment_parent' id='comment_parent' value='0' /> </p><p style="display: none;"><input type="hidden" id="akismet_comment_nonce" name="akismet_comment_nonce" value="1dc30d4928" /></p><p style="display: none;"><input type="hidden" id="ak_js" name="ak_js" value="177"/></p> </form> </div><!-- #respond --> </div><!-- #comments --> </main><!-- #main --> </div><!-- #primary --> <aside id="sidebar-primary" class="widget-area sidebar" role="complementary"> <section id="categories-2" class="widget widget_categories"><h2 class="widget-title">Categories</h2> <ul> <li class="cat-item cat-item-36"><a href="http://wordpressrelated.com/category/wordpress-htaccess/" title="Wordpress .htaccess">Wordpress .htaccess</a> </li> <li class="cat-item cat-item-25"><a href="http://wordpressrelated.com/category/wordpress-actions/" title="Wordpress actions">Wordpress actions</a> </li> <li class="cat-item cat-item-24"><a href="http://wordpressrelated.com/category/wordpress-admin/" title="Wordpress admin">Wordpress admin</a> </li> <li class="cat-item cat-item-38"><a href="http://wordpressrelated.com/category/wordpress-ajax/" title="Wordpress Ajax">Wordpress Ajax</a> </li> <li class="cat-item cat-item-40"><a href="http://wordpressrelated.com/category/wordpress-apache/" title="Wordpress Apache">Wordpress Apache</a> </li> <li class="cat-item cat-item-6"><a href="http://wordpressrelated.com/category/wordpress-categories/" title="Wordpress categories">Wordpress categories</a> </li> <li class="cat-item cat-item-22"><a href="http://wordpressrelated.com/category/wordpress-comments/" title="Wordpress comments">Wordpress comments</a> </li> <li class="cat-item cat-item-27"><a href="http://wordpressrelated.com/category/wordpress-css/" title="Wordpress css">Wordpress css</a> </li> <li class="cat-item cat-item-11"><a href="http://wordpressrelated.com/category/wordpress-custom-field/" title="Wordpress custom-field">Wordpress custom-field</a> </li> <li class="cat-item cat-item-2"><a href="http://wordpressrelated.com/category/wordpress-custom-post-types/" title="Wordpress custom-post-types">Wordpress custom-post-types</a> </li> <li class="cat-item cat-item-7"><a href="http://wordpressrelated.com/category/wordpress-custom-taxonomy/" title="Wordpress custom-taxonomy">Wordpress custom-taxonomy</a> </li> <li class="cat-item cat-item-15"><a href="http://wordpressrelated.com/category/wordpress-customization/" title="Wordpress customization">Wordpress customization</a> </li> <li class="cat-item cat-item-21"><a href="http://wordpressrelated.com/category/wordpress-filters/" title="Wordpress filters">Wordpress filters</a> </li> <li class="cat-item cat-item-8"><a href="http://wordpressrelated.com/category/wordpress-functions/" title="Wordpress functions">Wordpress functions</a> </li> <li class="cat-item cat-item-26"><a href="http://wordpressrelated.com/category/wordpress-html/" title="Wordpress html">Wordpress html</a> </li> <li class="cat-item cat-item-16"><a href="http://wordpressrelated.com/category/wordpress-images/" title="Wordpress images">Wordpress images</a> </li> <li class="cat-item cat-item-28"><a href="http://wordpressrelated.com/category/wordpress-javascript/" title="Wordpress javascript">Wordpress javascript</a> </li> <li class="cat-item cat-item-29"><a href="http://wordpressrelated.com/category/wordpress-jquery/" title="Wordpress jquery">Wordpress jquery</a> </li> <li class="cat-item cat-item-14"><a href="http://wordpressrelated.com/category/wordpress-loop/" title="Wordpress loop">Wordpress loop</a> </li> <li class="cat-item cat-item-12"><a href="http://wordpressrelated.com/category/wordpress-menus/" title="Wordpress menus">Wordpress menus</a> </li> <li class="cat-item cat-item-41"><a href="http://wordpressrelated.com/category/wordpress-mod-rewrite/" title="Wordpress mod-rewrite">Wordpress mod-rewrite</a> </li> <li class="cat-item cat-item-13"><a href="http://wordpressrelated.com/category/wordpress-multisite/" title="Wordpress multisite">Wordpress multisite</a> </li> <li class="cat-item cat-item-35"><a href="http://wordpressrelated.com/category/wordpress-mysql/" title="Wordpress Mysql">Wordpress Mysql</a> </li> <li class="cat-item cat-item-19"><a href="http://wordpressrelated.com/category/wordpress-pages/" title="Wordpress pages">Wordpress pages</a> </li> <li class="cat-item cat-item-17"><a href="http://wordpressrelated.com/category/wordpress-permalinks/" title="Wordpress permalinks">Wordpress permalinks</a> </li> <li class="cat-item cat-item-3"><a href="http://wordpressrelated.com/category/wordpress-php/" title="Wordpress PHP">Wordpress PHP</a> </li> <li class="cat-item cat-item-5"><a href="http://wordpressrelated.com/category/wordpress-plugin-development/" title="Wordpress plugin-development">Wordpress plugin-development</a> </li> <li class="cat-item cat-item-1"><a href="http://wordpressrelated.com/category/wordpress-plugins/" title="Wordpress plugins">Wordpress plugins</a> </li> <li class="cat-item cat-item-4"><a href="http://wordpressrelated.com/category/wordpress-posts/" title="Wordpress posts">Wordpress posts</a> </li> <li class="cat-item cat-item-39"><a href="http://wordpressrelated.com/category/wordpress-redirect/" title="Wordpress redirect">Wordpress redirect</a> </li> <li class="cat-item cat-item-23"><a href="http://wordpressrelated.com/category/wordpress-shortcode/" title="Wordpress shortcode">Wordpress shortcode</a> </li> <li class="cat-item cat-item-10"><a href="http://wordpressrelated.com/category/wordpress-theme-development/" title="Wordpress theme-development">Wordpress theme-development</a> </li> <li class="cat-item cat-item-18"><a href="http://wordpressrelated.com/category/wordpress-themes/" title="Wordpress themes">Wordpress themes</a> </li> <li class="cat-item cat-item-20"><a href="http://wordpressrelated.com/category/wordpress-widgets/" title="Wordpress widgets">Wordpress widgets</a> </li> <li class="cat-item cat-item-37"><a href="http://wordpressrelated.com/category/wordpress-woocommerce/" title="Wordpress Woocommerce">Wordpress Woocommerce</a> </li> <li class="cat-item cat-item-9"><a href="http://wordpressrelated.com/category/wordpress-wp-query/" title="Wordpress wp-query">Wordpress wp-query</a> </li> </ul> </section> </aside><!-- #secondary --> </div><!-- .inner-wrapper --></div><!-- .container --></div><!-- #content --> <footer id="colophon" class="site-footer" role="contentinfo"> <div class="container"> <div class="copyright"> Copyright © All rights reserved. </div><!-- .copyright --> <div class="site-info"> Business Point by <a href="https://promenadethemes.com" rel="designer">Promenade Themes</a> </div><!-- .site-info --> </div><!-- .container --> </footer><!-- #colophon --> </div><!-- #page --> <!-- AdSense Manager v4.0.3 (0.588 seconds.) --><a href="#page" class="scrollup" id="btn-scrollup"><i class="fa fa-angle-up"></i></a><link rel='stylesheet' id='yarppRelatedCss-css' href='http://wordpressrelated.com/wp-content/plugins/yet-another-related-posts-plugin/style/related.css?ver=4.9.8' type='text/css' media='all' /> <script type='text/javascript' src='http://wordpressrelated.com/wp-content/themes/wprelated/assets/js/navigation.js?ver=20151215'></script> <script type='text/javascript' src='http://wordpressrelated.com/wp-content/themes/wprelated/assets/js/skip-link-focus-fix.js?ver=20151215'></script> <script type='text/javascript' src='http://wordpressrelated.com/wp-content/themes/wprelated/assets/third-party/cycle2/js/jquery.cycle2.min.js?ver=2.1.6'></script> <script type='text/javascript' src='http://wordpressrelated.com/wp-content/themes/wprelated/assets/third-party/meanmenu/jquery.meanmenu.js?ver=2.0.2'></script> <script type='text/javascript' src='http://wordpressrelated.com/wp-content/themes/wprelated/assets/third-party/slick/slick.js?ver=1.6.0'></script> <script type='text/javascript' src='http://wordpressrelated.com/wp-content/themes/wprelated/assets/js/custom.js?ver=1.0.3'></script> <script type='text/javascript' src='http://wordpressrelated.com/wp-includes/js/wp-embed.min.js?ver=4.9.8'></script> <script async="async" type='text/javascript' src='http://wordpressrelated.com/wp-content/plugins/akismet/_inc/form.js?ver=4.0.3'></script> </body> </html>