dynamically inserting meta description into php page - php

ok so I've created this website and want to convert it to php just for fun. The website structure looks like any 'normal' web structure. like this:-
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body class="fish">
</body>
</html>
ok so i included from the head to the beginning of the body tag in header.php file. so header.php looks like this:- `
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body class="fish">`
now here is the problem. Each page should have it's own title, body class! and each page will also obviously have it's own meta description and content. How will I accomplish this guys? I was think of creating a function that base the meta description and body class on the page title. But is there a smatter way to accomplish this? Thanks

Either use a template engine or a MVC framework (such as CakePHP or CodeIgniter) which have template engines already incorporated in them.

Inside your header.php do something like this:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo $_tpl['title'] ?></title>
<meta name="description" content="<?php echo $_tpl['meta_desc'] ?>">
</head>
<body class="<?php echo $_tpl['body_class'] ?>">
On your page, before you use include('header.php'), define the vars as follows:
$_tpl = array();
$_tpl['title'] = 'My Title';
$_tpl['meta_desc'] = 'My meta description.';
$_tpl['body_class'] = 'fish';
As others have said though, don't re-invent the wheel. You'd be better to investigate some of the already-established templating engines for PHP:
Smarty
RainTPL

You should be creating a template to do this if it will be dynamic. You have many options on how you with to pass the data, whether it be a database, an object, array, etc. It is really tough to generate the data based on the page title, unless you are using a very persistent format to title each page.
<head>
<meta property="og:title" content="<?= $values['title'] ?>" />
<meta property="og:type" content="website" />
<meta property="og:url" content="<?= $values['url'] ?>" />
<meta property="og:image" content="<?= $values['image'] ?>/>
<meta property="og:site_name" content="<?= values['name'] ?>"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="ROBOTS" content="NOODP">
<link rel="icon" type="image/png" href="<?= $values['image'] ?>" />
<title><?= $values['title'] ?></title>
<? if(isset($values['css'])) : ?>
<? foreach($values['css'] as $css) : ?>
<link href="/css<?= $css['data'] ?>" rel="stylesheet" type="text/css" />
<? endforeach ?>
<? endif ?>
<? if(isset($values['js'])) : ?>
<? foreach($values['js'] as $js) : ?>
<script src="/js<?= $js['data'] ?>" type="text/javascript"></script>
<? endforeach ?>
<? endif ?>
</head>

Related

How can I remove the <meta http-equiv="content-type" content="text/html; charset=utf-8" /> of Joomla?

How can i remove the <meta http-equiv="content-type" content="text/html; charset=utf-8" />
on this PHP Joomla Template? Because I have a Duplication of the Meta Tag from Joomla and from the Template :/ The Head.php file it is calling is:
<meta charset="<?php echo $this['system']->document->getCharset(); ?>">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<?php if($this['config']->get('responsive', true)): ?>
<meta name="viewport" content="width=device-width, initial-scale=1">
<?php endif; ?>
<?php if (isset($error)): ?>
<title><?php echo $error; ?> - <?php echo $title; ?></title>
<?php else: ?>
<jdoc:include type="head" />
<?php endif; ?>
and the Index.php is:
// get theme configuration
include($this['path']->path('layouts:theme.config.php'));
?>
<!DOCTYPE HTML>
<html lang="<?php echo $this['config']->get('language'); ?>" dir="<?php echo $this['config']->get('direction'); ?>" data-config='<?php echo $this['config']->get('body_config','{}'); ?>'>
<head>
<?php echo $this['template']->render('head'); ?>
</head>
<body class="<?php echo $this['config']->get('body_classes'); ?>">
here is an Image of this Problem:
Link to the Problem ( imgur )
If i understand your problem right. Do you want to remove the duplicate chartset?
Simple remove the first line in your first code sample.
And you can try $doc->setHtml5(true);in the index.php

What is the best way to Include meta tags in the head section of a PHP template?

I created a simple php template.
In the header.php I have the following lines of code:
<!DOCTYPE HTML>
<html lang="en"><head>
<meta charset="utf-8">
<title><?php echo $title ?></title>
<meta name="description" content="<?php echo $description ?>">
<link rel="canonical" href="<?php echo $canonical ?>">
</head><body>
here rest of code (header, menu etc...)
On all pages I have the following lines of code at the top:
<?php
$title="";
$description="";
$canonical="";
$page_schema="http://schema.org/WebPage";
$INC_DIR = $_SERVER["DOCUMENT_ROOT"]. "/includes/";
require($INC_DIR. "header.php"); ?>
I have pages with Open Graph meta tags and some pages who don't have Open Graph meta tags.
I have pages with
<link rel="alternate" hreflang="nl" href="">
and pages that don't use this.
I have 2 pages with robots meta tag noindex follow and many pages who don't use any robots meta tag.
Now i'm using the following option:
I've added the following line in the header.php
<?php echo $meta_tags ?>
Example:
<!DOCTYPE HTML>
<html lang="en"><head>
<meta charset="utf-8">
<title><?php echo $title ?></title>
<meta name="description" content="<?php echo $description ?>">
<link rel="canonical" href="<?php echo $canonical ?>">
<?php echo $meta_tags ?>
And the following lines on pages (not all use hreflang tag):
$meta_tags='<link rel="alternate" hreflang="nl" href="">
<meta property="og:image" content="">
<meta property="og:title" content="">
<meta property="og:description" content="">
<meta property="og:url" content="">';
Example:
<?php
$title="";
$description="";
$canonical="";
$meta_tags='<link rel="alternate" hreflang="nl" href="">
<meta property="og:image" content="">
<meta property="og:title" content="">
<meta property="og:description" content="">
<meta property="og:url" content="">';
$page_schema="http://schema.org/WebPage";
$INC_DIR = $_SERVER["DOCUMENT_ROOT"]. "/includes/";
require($INC_DIR. "header.php"); ?>
Is this a good solution to include some meta tags on pages that do use them? Or is there a better way?
Like adding NULL (without quotes) to empty variables like: $title=NULL;
Example (for a few pages that don't use opengraph):
header.php:
<meta property="og:title" content="<?php echo $og_title ?>">
<meta property="og:description" content="<?php echo $og_description ?>">
<meta property="og:url" content="<?php echo $og_url ?>">';
on pages (which don't use open graph tags):
$og_title=NULL;
$og_description=NULL;
$og_url=NULL;
What is the best solution? Is there a better solution?
Btw... I was following the steps of this tutorial here: http://www.heliomedia.com/tutorials/html5-template-with-seo-friendly-variables/
Yes, omit the variables you're not using in each page. In your head include, wrap each meta tag in a check to see if it's used, e.g.:
<?php if (isset($og_title)) {?>
<meta property="og:title" content="<?php echo htmlspecialchars($og_title);?>">
<?php }?>

Define in PHP not accept space

I use define() function to make settings page for my site.
In my settings.php
define('DESCRIPTION', "Admin Dashboard Template");
define('TITLE', "Modern");
define('KEYWORDS', "admin,dashboard");
define('AUTHOR', "Minh Tan");
In my index.php
<?php include('config/settings.php'); ?>
<!DOCTYPE html>
<html>
<head>
<!-- Title -->
<title><?php echo TITLE; ?> | Login - Sign in</title>
<meta content="width=device-width, initial-scale=1" name="viewport"/>
<meta charset="UTF-8">
<meta name="description" content=<?php echo DESCRIPTION; ?> />
<meta name="keywords" content=<?php echo KEYWORDS; ?> />
<meta name="author" content=<?php echo AUTHOR; ?> />
But when i run on my browser. It make this results:
Help me! Thanks.
Wrap your PHP statements in double quotes.. they go missing after your charset.
<meta name="description" content="<?php echo DESCRIPTION; ?>" />
<meta name="keywords" content="<?php echo KEYWORDS; ?>" />
<meta name="author" content="<?php echo AUTHOR; ?>" />

joomla 3.x - how to include "metadescription" and "title" in the header without using <jdoc:include type="head" />

I am trying to get more control over the header of my Joomla site; for some pages I don't need many things in the header. I decided to make a template where I don't use the <jdoc:include type="head" />, because it loads lot of things that I don't need.
Searching, I found this old post about the subject, and in the web some people looking for the same thing. Manually control <head> markup in Joomla
I was wondering if it is possible to add to my index.php template file to PHP code that could get just the "metadescription" and the "title" of the Joomla publication. Something like this:
<?php defined( '_JEXEC' ) or die; ?>
<!doctype html>
<html lang="<?php echo $this->language; ?>">
<head>
<meta name="viewport" content="width=device-width />
<meta name="description" content="<?php echo **code metadescription** ?>" />
<title><?php echo **code to get title** ?></title>
</head>
<body>
<jdoc:include type="component" />
</body>
</html>
Nice, after while i could find the code that i was looking for, and maybe it could help others, it worked for me... in the index.php file of the template i added:
<?php defined( '_JEXEC' ) or die;
$doc =JFactory::getDocument();
$meta_description = $doc->getMetaData("description");
$title = $doc->getTitle();
?>
<!doctype html>
<html lang="<?php echo $this->language; ?>">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" />
<meta name="description" content="<?php echo "$meta_description"; ?>" />
<title><?php echo "$title" ?></title>
</head>
<body> <jdoc:include type="component" /> </body>
</html>
Just use PHP include() function
In top.php
<meta name="viewport" content="width=device-width />
<meta name="description" content="<?php echo **code meta description** ?>" />
<title><?php echo **code to get title** ?></title>
And in your current file just include the file(top.php) like
<?php defined( '_JEXEC' ) or die; ?>
<!doctype html>
<html lang="<?php echo $this->language; ?>">
<head>
<?php include("top.php"); ?>
</head>
<body>
<jdoc:include type="component" />
</body>
</html>
I don't know if this is a good way, but you can unset all css and js in the following style:
unset($doc->_styleSheets[$this->baseurl.'/path/to/some.css']);
unset($doc->_scripts[$this->baseurl.'/path/to/some.js']);
I recommend not to remove meta tage like content-type or x-ua-compatible. These tags support your website in some browsers. And the favicon link is helpful by bookmarks.

HEAD section in a PHP include

I work with PHP includes, and I need to put HEAD information in one of them. Is this possible, or can I only put a HEAD section on top of the index.php?
I'm asking this because the PHP includes has queries which I need in order to get OG image data (for social media) into the head. For example: I have a file WEBSHOP.PHP and in this file there is a product with an image. I want that image to show on the timeline in FaceBook.
This is an example of my (shortened version) of index.php:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
<body>
<? include webshop.php; ?>
</body>
This is an example of my (shortened version) of webshop.php:
<!-- some mysql query to get variables as $pic and $row->meta_title -->
<head>
<meta property="og:image" content="http://forteuitgevers.nl/images/boeken/<? echo $pic; ?>" />
<meta property="og:title" content="<? echo $row->meta_title; ?>" />
<meta property="og:description" content="<? echo $row->meta_des; ?>" />
<meta property="og:url" content="http://<? echo $_SERVER['HTTP_HOST']; ?>/<? if (!empty($url_array[1])) { echo $url_array[1]; echo '/' ; } ?><? if (!empty($url_array[2])) { echo $url_array[2] ; } ?>" >
</head>
<!-- some code to view the webshop item -->
You're going to have to change the structure of your PHP files a bit in order to get all the header tags into one <head> section. If you include the webshop.php file before you start generating your HTML output you can then access the PHP variables when you write the head section. Something like this:
index.php:
<?php include webshop.php; ?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<meta property="og:title" content="<?php echo $row->meta_title; ?>" />
<!-- other meta tags using variables from webshop.php -->
</head>
<body>
<!-- print out HTML code from webshop.php -->
<?php echo $doc_body; ?>
</body>
Then in webshop.php you'll have to save any HTML output with output buffering so you can add it into the HTML code in the proper place. Something like this:
<?php
// sql queries to get data
ob_start();
?>
<!-- html code to show up in the body section to view webshop items -->
<?php
$doc_body = ob_get_clean();
?>
Check out the PHP.net manual page on Output buffering for more info on ob_start and ob_get_clean.
Yes you can. However this is bad style. And you are making your HTML wrong:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
<body>
<? include webshop.php; ?>
</body>
this will lead into
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
<body>
<head>
<meta property="og:image" content="http://forteuitgevers.nl/images/boeken/<? echo $pic; ?>" />
<meta property="og:title" content="<? echo $row->meta_title; ?>" />
<meta property="og:description" content="<? echo $row->meta_des; ?>" />
<meta property="og:url" content="http://<? echo $_SERVER['HTTP_HOST']; ?>/<? if (!empty($url_array[1])) { echo $url_array[1]; echo '/' ; } ?><? if (!empty($url_array[2])) { echo $url_array[2] ; } ?>" >
</head>
</body>
However HTML does not like that the head tag is inside of the body tag. But most browser will still show it correctly.
To be sure: Check your result with a HTML Validator.

Categories