How can I add a different title, keyword and description in every page's <head> of my simple php website dynamically?
I have included file header.php in all of my pages, how can i know in what page the user in?
For example, I have php files register.php, and login.php, and I need different titles, keywords and descriptions.
I do not want use the $_GET method.
Thanks!
Set variables at the top of each page that will be read by header.php. Then insert the values of the variables in the right places in header.php. Here is an example:
register.php:
<?php
$title = "Registration";
$keywords = "Register, login";
$description = "Page for user registration";
include('header.php');
?>
header.php
<html>
<head>
<meta name="keywords" content="<?php echo $keywords; ?>" />
<meta name="description" content="<?php echo $description; ?>" />
<title><?php echo $title; ?></title>
</head>
<body>
Put the output inside a function (within your header.php) and insert its parameters at appropriate places into the markup.
function html_header($title = "Default") {
?><!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title><?php echo $title ?></title>
</head>
…
<?php
}
you can try this:
for example the $page variable is your page name:
<?php
switch($page)
{
case 'home':
$title = 'title';
$keyword = 'some keywords..';
$desc = 'description';
break;
case 'download':
$title = 'title';
$keyword = 'some keywords..';
$desc = 'description';
break;
case 'contact':
$title = 'title';
$keyword = 'some keywords..';
$desc = 'description';
break;
}
if(isset($title))
{
?>
<title><?php echo $title; ?></title>
<meta name="keywords" content="<?php echo $keyword; ?>" />
<meta name="description" content="<?php echo $desc; ?>" />
<?php
}
else
{
?>
<title>default</title>
<meta name="keywords" content="default" />
<meta name="description" content="default" />
<?php
}
?>
Related
Hello everyone.
I've got a small problem. Instead of making a $content variable and putting in the whole content there, I would like to set the page up like this (below), though the problem is that the php variables that is placed at the end (title, keywords, description, page), aren't working even though its in the index.php.
Question: How can I make the variables work without making a few head_template.php with the variables filled out already?
index.php:
<!DOCTYPE html>
<html class="html" lang="en">
<?php require_once "head_template.php"; ?>
<body class="<?php echo $page; ?>">
<?php require_once "navigation_menu_template.php"; ?>
<?php require_once "load_template.php"; ?>
<h1>Content</h1>
<?php require_once "cookies_script_template.php"; ?>
<?php require_once "footer_template.php"; ?>
</body>
</html>
<?php
$title = "QCG | Homepage";
$keywords = "QCG, Homepage";
$description = "QCG | Homepage - Description";
$page = "homepage";
?>
head_template.php:
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="UTF-8">
<meta name="description" content="<?php echo $description; ?>">
<meta name="keywords" content="<?php echo $keywords; ?>">
<title><?php echo $title; ?></title>
<link rel="stylesheet" type="text/css" href="css/css.css">
</head>
You are setting them after they are used. If you put that block at the top of your page it will work.
<!DOCTYPE html>
<html class="html" lang="en">
<?php
$title = "QCG | Homepage";
$keywords = "QCG, Homepage";
$description = "QCG | Homepage - Description";
$page = "homepage";
?>
<?php require_once "head_template.php"; ?>
<body class="<?php echo $page; ?>">
<?php require_once "navigation_menu_template.php"; ?>
<?php require_once "load_template.php"; ?>
<h1>Content</h1>
<?php require_once "cookies_script_template.php"; ?>
<?php require_once "footer_template.php"; ?>
</body>
</html>
I would like to write a function to return a string with html code to customize head title, description and keywords for multiple pages. I started with my index.php file and two auxiliary, _head.php and _functions.php. What do I have to do to implement this function?
index.php:
<?php include "_functions.php; ?>
<html>
<?php echo make_head("My title", "My description", "My keywords); ?>
<body>
...
</body>
</html>
_functions.php:
function make_head(title, description, keywords) {
return file_get_contents("_head.php");
}
_head.php
<head>
...
<meta name="description" content="$description">
<meta name="keywords" content="$keywords">
<title>$title</title>
...
</head>
_functions.php:
function make_head($title, $description, $keywords) {
$head = include "_head.php";
return $head
}
_head.php
<head>
...
<meta name="description" content="<?php echo $description; ?>" >
<meta name="keywords" content="<?php echo $keywords; ?>" >
<title><?php echo $title; ?></title>
...
</head>
You can use include.
In your function make_head, you can do something like this:
function make_head(title, description, keywords) {
$html = include "_head.php";
return $html;
}
When you include something, it loads it to your current state. So if you use $title or $description or $keywords in your _head.php file, they will be in the same scope and they can be used.
This code has a number of vulnerabilities and including files like this is never a good idea. I can see this code is only entry level though and probably just for practice so to get this working you need to do something like the following:
<?php include "_functions.php; ?>
<html>
<?php echo make_head("My title", "My description", "My keywords); ?>
<body>
Then in your make_head function, return the HTML code
function make_head(title, description, keywords) {
return "<head>
...
<meta name='description' content=' . "$description" .'>
<meta name='keywords' content=' . "$keywords" . '>
<title>$title</title>
...
</head>";
}
This approach leave you open and I certainly wouldn't use it in production.
Is there a way to use page $data variables in the template it's loaded in?
For instance - if I have a template that has a head like this:
<!DOCTYPE html>
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
<head>
<title><?php echo $data['title']; ?> | ACME, Inc.</title>
</head>
My view is pages/home and the controller looks something like this:
public function index($data) {
$data = array();
$data['title'] = 'Home';
$this->load->view('pages/home', $data);
}
How do I get a parent template or sibling view to take on the page's variables?
I usually use the code below for meta data in page
In controller:
$page = new stdClass();
$page->title = "Your Page Title";
$page->desc = "Your page description";
$page->key = "keword1,keyword2,keyword3";
$page ->author = "Alex";
$data['page'] = $page;
$this->load->view('pages/home', $data);
In View:
<title><?php
if(isset($page->title)){
echo $page->title;
}
?></title>
<meta name="description" content="<?php
if(isset($page->desc)){
echo $page->desc;
}
?>"/>
<meta name="keywords" content="<?php
if(isset($page->key)){
echo $page->key;
}
?>"/>
<meta name="author" content="<?php
if(isset($page->author)){
echo $page->author;
}
?>"/>
content="<?php
if(isset($page->desc)){
echo $page->desc;
}
?>"/>
This is safe for not conflicting variable and easy to use.
I used seo validator to check my website, but it says my website does not have site title. I have these header codes below:
<head profile="http://gmpg.org/xfn/11">
<meta name="google-site-verification" content="RR4KnVDyxKEeBxQg8Qwphb0E2RuBTxVE3y-NrLS_tsw" />
<meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php bloginfo('charset'); ?>" />
<title><?php bloginfo('name'); ?> <?php if ( is_single() ) { ?> » <?php } ?> <?php wp_title(); ?></title>
<meta name="Description" content="Davao Property For Sale is the desired partner of Filipino families in finding and owning a HOME in Davao City, Philippines every step of the way." />
<meta name="Keywords" content="Davao City, real estate, House for sale, rush house, brand new, ready for occupancy, Davao Property" />
Please help!
Try this:
<title>
<?php
$title = bloginfo('name');
if ( is_single() ) {
$title .= '»';
}
$title .= wp_title();
echo $title;
?>
</title>
The idea is to use the variable $title to concatenate your title and finally write it with function echo.
<title><?php echo bloginfo('name').(is_single()?' »':'').' '.wp_title() ?></title>
or
<title><?= bloginfo('name').(is_single()?' »':'').' '.wp_title() ?></title>
or
<?php $pageT = bloginfo('name');
if (is_single()) $pageT .= ' »';
$pageT .= ' ' . wp_title(); ?>
<title><?php echo $pageT ?></title>
There are some other possibilities to write this, though.
How can I add a different title, keyword and description in every page's <head> of my simple php website dynamically?
E.g
<title>this is title</title>
<meta name="keywords" content="keyword1, keyword2" />
<meta name="description" content="this is description" />
You can create method to return title for current page, depending on where the user is, and then use it like this.
<title><?php echo get_title(); ?></title>
same with the keywords
<meta name="keywords" content="<?php echo get_keywords(); ?>" />
<meta name="description" content="<?php echo get_description(); ?>" />
The implementation will depend on how you navigate on the website. For example, if you have only index.php, and choose content by $_GET["page"], you can have something like this
function get_title() {
switch($_GET["page"]) {
case "home":
return "Welcome to my home page";
case "guestbook":
return "Welcome to guestbook";
}
}
or you can make it all in one like
function get_headers() {
// here set $title, $description and $keywords according to current page
// ....
// then just generate html
$html = "<title>$title</title>";
$html .= "<meta name='description' content='$description' />";
$html .= "<meta name='keywords' content='$keywords' />";
return $html;
}
and then again do something like this
<head>
...
<?php echo get_headers(); ?>
...