Set Page Title Using PHP

How to change title of the page after including header.php?

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;
?>

How to change TITLE dynamically after the header is included

You cant do that without a nasty hack.

What you should do is perform all your logic BEFORE you output html. A simple example follows:

<?php
//index.php
//perform logic and set variables before any html

$page = isset($_GET['menu'])?$_GET['menu']:'home';

switch($page){
case 'home':
$title = ' welcome to myco.ltd';
$content = 'pages/home.php';
break;
case 'about':
$title = 'about us';
$content = 'pages/about.php';
break;
case 'contact':
$title = 'get in touch';
$content = 'pages/contact.php';
break;
}
//the following html could be in a separate file and included, eg layout.php
?>
<html>
<head>
<title><?php echo $title;?></title>
</head>
<body>
<!--menu and other shared html here-->
<?php include $content;?>
<!-- shared footer stuff here-->
</body>
</html>

This is essentially a VERY barebones router script, an essential component of any framework. I would highly recommend you consider a lightweight framework rather than write everything from scratch. http://fatfreeframework.com/home would be a great start

Set page title after head tags

Another solution

you can do this

index.php

<html>
<head>
<title><?php title_func(); ?></title>
</head>

<body>
require "content.php";
</body>
</html>

content.php
Just make this function anywhere

 <?php 
function title_func(){
echo "title here";
}
?>

Fastest way to retrieve a title in PHP

<?php
function page_title($url) {
$fp = file_get_contents($url);
if (!$fp)
return null;

$res = preg_match("/<title>(.*)<\/title>/siU", $fp, $title_matches);
if (!$res)
return null;

// Clean up title: remove EOL's and excessive whitespace.
$title = preg_replace('/\s+/', ' ', $title_matches[1]);
$title = trim($title);
return $title;
}
?>

Gave 'er a whirl on the following input:

print page_title("http://www.google.com/");

Outputted: Google

Hopefully general enough for your usage. If you need something more powerful, it might not hurt to invest a bit of time into researching HTML parsers.

EDIT: Added a bit of error checking. Kind of rushed the first version out, sorry.

How to set page title with php conditional

With match expression introduced in php 8 more effective and elegant way than conditions if-elseif... I hope this simplify your code and solve the problem.

<?php
$page = match(basename($_SERVER['PHP_SELF'])) {
'index.php' => 'Home page',
'price.php' => 'Price page',
'areas.php' => 'Areas page',
default => 'Unknown page'
};
?>
<title><?=$page?></title>

Set page title in WordPress with PHP

I did some research with my template. My template calls get_header() to print the HTML tags with head and so on, and I guess yours does the same.

To replace the title, I start a output buffering right before calling that function and get it afterwards.

After that, I can replace the title easily with preg_replace():

ob_start();
get_header();
$header = ob_get_clean();
$header = preg_replace('#<title>(.*?)<\/title>#', '<title>TEST</title>', $header);
echo $header;

How can I change title using php?

You can do something like this:

index.php:

<?php 
$pagename = 'Toys-Home';
include ('headernav.php');?>
<div class="header-outs" id="home">
<div class="header-bar">
<div class="info-top-grid">
<div class="info-contact-agile">
<ul>
<li>
<span class="fas fa-phone-volume"></span>
<p>+(000)123 4565 32</p>
</li>
<li>
<span class="fas fa-envelope"></span>
<p><a href="mailto:info@example.com">info@example1.com</a></p>
</li>
<li>
</li>
</ul>
</div>
</div>
</div>
</div>

headernav.php

<!DOCTYPE html>
<html lang="zxx">
<head>
<title><?php print $pagename; ?></title>
<!--meta tags -->
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="keywords" content="" />
<link href="css/style.css" rel='stylesheet' type='text/css' media="all">
<!--//stylesheets-->
<link href="//fonts.googleapis.com/css?family=Sunflower:500,700" rel="stylesheet">
<link href="//fonts.googleapis.com/css?family=Open+Sans:400,600,700" rel="stylesheet">
</head>
<body>

You'd have to set the $pagename variable on each page if that's something you're looking for. Each page would have the title you've set in that variable.

Change page title using javascript inside a PHP 'if'

Simply adding the script to the DOM won't cause it to execute. So you need to call it somehow, maybe by wrapping it in a function and calling it by name.

However, if you are going to do that, you may as well echo only the newTitle and update it after you do the http request:

JS:

<!-- Assuming you have something like this -->
<script>
var xhttp = new XMLHttpRequest();

xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
var responseText = xhttp.responseText;
document.title = responseText;
}
};

xhttp.open("GET", "index.php?rq=" + rq , true);
xhttp.send();

</script>

PHP:

if (isset($newTitle)){
echo($newTitle);
}


Related Topics



Leave a reply



Submit