18

page.php:

<?php
include("header.php");
$title = "TITLE";
?>

header.php:

<title><?php echo $title; ?></title>

I want my title to be set after including the header file. Is it possible to do this?

2
  • 10
    The best way to go would be to change your program's logic so you can define the title in time before outputting the title tag. There are workarounds but they are kludgy.
    – Pekka
    Commented Oct 22, 2012 at 10:15
  • Related: stackoverflow.com/questions/2825776/…
    – JNF
    Commented Oct 22, 2012 at 10:16

9 Answers 9

42

expanding on Dainis Abols answer, and your question on output handling,

consider the following:

your header.php has the title tag set to <title>%TITLE%</title>; the "%" are important since hardly anyone types %TITLE% so u can use that for str_replace() later.

then, you can use output buffer like so

<?php
    ob_start();
    include("header.php");
    $buffer=ob_get_contents();
    ob_end_clean();

    $buffer=str_replace("%TITLE%","NEW TITLE",$buffer);
    echo $buffer;
?>

and that should do it.

EDIT

I believe Guy's idea works better since it gives you a default if you need it, IE:

  • The title is now <title>Backup Title</title>
  • Code is now:
<?php
    ob_start();
    include("header.php");
    $buffer=ob_get_contents();
    ob_end_clean();

    $title = "page title";
    $buffer = preg_replace('/(<title>)(.*?)(<\/title>)/i', '$1' . $title . '$3', $buffer);

    echo $buffer;
?>
2
  • is this method seo friendly?? Commented Jun 12, 2014 at 17:43
  • 2
    yes and no. if you change a title every few days search engines wont like it, but having a different title for every page is definitely seo friendly.
    – we.mamat
    Commented Jul 8, 2014 at 22:41
21

1. Simply add $title variable before require function

<?php

$title = "Your title goes here";
require("header.php");

?>

2. Add following code into header.php

<title><?php echo $title; ?></title>
0
10

What you can do is, you store the output in a variable like:

header.php

<?php
   $output = '<html><title>%TITLE%</title><body>';
?>

PS: You need to remove all echos/prints etc so that all possible output is stored in the $output variable.

This can be easely done, by defining $output = ''; at the start of the file and then find/replace echo to $output .=.

And then replace the %TITLE% to what you need:

<?php
    include("header.php");
    $title = "TITLE";

    $output = str_replace('%TITLE%', $title, $output);
    echo $output;
?>

Another way is using javascript in your code, instead of:

<title><?php echo $title; ?></title>

Put this in there:

<script type="text/javascript">
    document.title = "<?=$title;?>"
</script>

Or jQuery, if you prefer:

<script type="text/javascript">
    $(document).ready(function() {
        $(this).attr("title", "<?=$title;?>");
    });
</script>
4
  • the first variant doesn't seem to work. and then, if i use js, page title changes, but if you watch the source code it's not what i need Commented Oct 22, 2012 at 10:27
  • well, actually, the first variant works, but it puts page title outside of the header area, because echo goes not in header.php page Commented Oct 22, 2012 at 10:30
  • You will need to remove all echos in header.php and write $output .= wherever you have echo. That's the idea of the script.
    – Peon
    Commented Oct 22, 2012 at 10:31
  • Oh, and JS will always change clients side view, not the actual source code.
    – Peon
    Commented Oct 22, 2012 at 10:32
7

Expanding a little on we.mamat's answer, you could use a preg_replace instead of the simple replace and remove the need for a %title% altogether. Something like this:

<?php
ob_start();
include("header.php");
$buffer=ob_get_contents();
ob_end_clean();

$title = "page title";
$buffer = preg_replace('/(<title>)(.*?)(<\/title>)/i', '$1' . $title . '$3', $buffer);

echo $buffer;
?>
4

you can set using JavaScript

<script language="javascript">
    document.title = "The new title goes here.";
</script>
1
  • 1
    nice but it has 2 drawbacks. one is not use for seo because this code not write title tag in header. second is how to add meta description and keywords? Commented Nov 9, 2013 at 5:28
3

Add this code on top your page

<?php
    $title="This is the new page title";
?>

Add this code on your Template header file (include)

<title><?php echo $title; ?></title>
0
2

It's very easy. Put this code in header.php

        <? 
$sitename = 'Your Site Name'
$pagetitle;

if(isset($pagetitle)){
 echo "<title>$pagetitle." | ". $sitename</title>";
} 
  else {
echo "<title>$sitename</title>";
}
?>

Then in the page put there :

 <?
    $pagetitle = 'Sign up'
    include "header.php";
    ?>

So if you are on Index.php , The title is Your Site Name. And for example if you are on sign up page , The title is Sign up | Your Site Name

1

Every Simple just using a function , I created it .

   <?
function change_meta_tags($title,$description,$keywords){
        // This function made by Jamil Hammash
    $output = ob_get_contents();
    if ( ob_get_length() > 0) { ob_end_clean(); }
    $patterns = array("/<title>(.*?)<\/title>/","<meta name='description' content='(.*)'>","<meta name='keywords' content='(.*)'>");
    $replacements = array("<title>$title</title>","meta name='description' content='$description'","meta name='keywords' content='$keywords'");

    $output = preg_replace($patterns, $replacements,$output);  
    echo $output;
}
?>

First of all you must create function.php file and put this function inside ,then make require under the MetaTags in Header.php . To use this function change_meta_tags("NEW TITLE","NEW DESCRIPTION",NEW KEYWORDS); . Don't use this function in Header.php !! just with another pages .

1
  • Just make sure you place right sequence first title, description, keywords. otherwise this function won't work, secondly use single quotation for description and keywords. and yeah very handy function indeed
    – Saad Suri
    Commented Sep 1, 2017 at 7:18
0

Use a jQuery function like this:

$("title").html('your title');

1
  • this is a valid solution, but a bad idea, SEO wise. The title will not be changed if no javascript is loaded (all bots do not load page javascript)
    – we.mamat
    Commented Mar 31, 2018 at 15:42

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.