Change title in browser tab

10.6k Views Asked by At

I'm trying to change the title in browser tab for a child theme of twentytwelve. I wan't it to print out the same title on every page. What the heck is happening?

    <?php
    function im_awesome_title($title){
      $title = "Im awesome!";
    return $title;
    }
    add_filter( 'wp_title', 'im_awesome_title', 20 );
2

There are 2 best solutions below

0
On

Your im_awesome_title($title) function will always return the value of $title when invoked. To print this title on every page, you will need to call the function in the title meta tag of your pages. So it will look like this:

<head>
<title><?php echo im_awesome_title($title); ?></title>
</head>

Of course your function expects an argument, so if you want the same title on every page, it's best to leave out the argument and simply set your $title variable to whatever page title that you want to use on all your pages.

Hope this helps.

0
On

or some like this. script better put in the end of all php tags

<?php
//set title
echo "<script>setTimeout(function(){var tts = document.getElementsByTagName(\"title\");if(tts.length > 0)tts[0].innerHTML=\"My title\"; else {var tt0 = document.createElement(\"TITLE\"); tt0.innerHTML = \"My title\"; document.head.appendChild(tt0);}}, 200);</script>";
?>