PHP Auto Website Title

By Jonesy44 on Apr 28, 2008

This simple PHP function is pretty handy if you want your websites pages in the title without having to name individual ones.

It will look like:

MyWebsite | Index
MyWebsite | Gallery
MyWebsite | Forums

paste the code into your "functions" file, or any webpage which is included in every page you have
(Reminder: To include a webpage, use 'include("webpage.php");')
You can of course put the function in each page, but that's just annoying .. :P

then, in each webpage you want the title to appear, put


I've quoted each line to show the people who don't do PHP, how the function works a little better

Rate it, post comments :P

function title() {
  $page = $_SERVER["PHP_SELF"];
  // Set variable $page as the current webpage
  $page = str_replace(".php","",$page);
  // Remove the .php ending from the string: $page
  $page = str_replace("/","",$page);
  // Remove the / before the website name
  $page = ucwords($page);
  // Capitalize the first letters
  echo '<title>MyWebsite | ' .$page. '</title>';
  // Change "MyWebsite" to your website name


Sign in to comment.
Jonesy44   -  Apr 29, 2008

\'kay, will do. thanks for the info on that :P
I always thought case\'s / break\'s were JS for some reason :s

Anyways, i\'ll have a look into it, and perhaps release a better version xD

Hawkee   -  Apr 28, 2008

Sure, or use it as a template variable. I use templates so my PHP code doesn\'t mix with any html. Look into the Smarty template engine to see what I mean.

Jonesy44   -  Apr 28, 2008

then put

\' .$title. \'


Hawkee   -  Apr 28, 2008

Something like:

    case \"index\":
        $title = \"Homepage Title\";

    case \"about\":
        $title = \"About Us Title\";
Jonesy44   -  Apr 28, 2008

I can agree to that, you\'re a hard one to compete with, Hawkee :P
But, learn from the best eh? ;-)

what do yoou mean by a switch statement? :-s

Hawkee   -  Apr 28, 2008

I think its better to use a switch statement to tie the file names to better page titles that you define separately. If you only use the words in the file names your page titles will be very short and limited. In other words this won\'t help you rank in the search engines very well.

Jonesy44   -  Apr 28, 2008

Feel free to post any feedback

Bummp to the comments page xD

Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.