Is there a way to add page titles and meta tags dynamically using PHP (and MySQL)?

Ryan Sacks
Ryan Sacks used Ask the Experts™
Hi,  I'm creating a PHP and MySQL web application for a client. I'm a fairly new web developer.
Is there a way to add page titles and meta tags dynamically? It's a small application. I'm not using a Framework.
I saw a couple of possible solutions on google, but am not sure what the easiest and most efficient way of doing it would be.

Possible Solution #1, add variables in my header.php page and then on each respective page for example about, contact etc... add those variables above the include statement.
Here is my header.php code:


<!DOCTYPE html>
<html lang="en">
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<meta name="description" content="<?php echo $meta_description; ?>">
	<meta name="author" content="Ryan Sacks">
	<link rel="icon" href="img/favicon.ico">
	<title><?php echo $page_title; ?></title>
	<!-- Bootstrap core CSS -->
	<link href="css/bootstrap.min.css" rel="stylesheet">
	<!-- Font Awesome Icons -->
	<link rel="stylesheet" href="" integrity="sha384-mzrmE5qonljUremFsqc01SB46JvROS7bZs3IO2EmfFsd15uHvIt+Y8vEf7N7fWAU" crossorigin="anonymous">
	<!-- Custom CSS -->
	<link href="css/style.css?v=<?php echo time(); ?>" rel="stylesheet">
	<!-- Google Fonts -->
	<link href=",400,500" rel="stylesheet">
	<link href=",400,700,900" rel="stylesheet">
	<link href=",700" rel="stylesheet">
	<!-- Animate CSS -->
	<link rel="stylesheet" href="">
  	<!-- Breadcrumbs CSS -->
	<link rel="stylesheet" href="css/breadcrumbs.css">
	<!-- HTML5 shiv and Respond.js IE8 support of HTML5 elements and media queries -->
	<!--[if lt IE 9]>
	<script src=""></script>
	<script src=""></script>
	<!-- ReCaptcha v2 -->
	<script src=''></script>

Open in new window

and in my index.php:
$page_title = "Hockey Pool Fundraiser | A fun and simple way to save thousands of dollars for your team";

$meta_description = "A simple pool-style Hockey program";


Open in new window

Possible Solution #2, I saw something about creating a function and using a switch statement but I'm not exactly sure what that code would look like. I have about 20 pages in the application.
Can anyone help?
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
David FavorFractional CTO
Distinguished Expert 2018

Tip: You can use WordPress + you'll solve your current requirement + many more.

Or, you can download WordPress + go through their code to see how they approach solving your requirement + just copy... er... I mean... use their code as an example to tool some similar code...
Ryan SacksGraphic and Web Designer / Developer


My client didn’t want to use WordPress for 2 reasons.
1) we would’ve needed to buy a plan for a Form Plugin and he didn’t want to pay $30 USD a month forever plus Add Ons.
2) He didn’t want to rely on a 3rd party Plugin and
3) I wasn’t comfortable using Hooks and Filters.

WordPress uses custom functions and other code only specific to WordPress. So its not the exact same code as building a php application Without using WordPress. Ps. I know WordPress is awesome and powers about 35% of all websites.

I think a php developer should be able to help me.
Well, when I need to do this I usually do something like (using your code as an example):

<!DOCTYPE html>
<html lang="en">
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
         <? $qpgval="somepage1"; include ("dynmeta"); ?>

-- continued html --

Open in new window

dynmeta (quick example)

<?php if(isset($qpgval) && ){if ($qpgval == "somepage1"){?>
                <title>Somepage1 title</title>
                <meta charset="utf-8">
                <meta name="description" content="page description"/>
                <meta name="keywords" content="some keywords"/>

<?php }else if($qpgval == "somepage2"){?>
                <title>Somepage2 title</title>
                <meta charset="utf-8">
                <meta name="A different page description"/>
                <meta name="keywords" content="Different keywords"/>


Open in new window

Hope it helps
Scott FellDeveloper & EE Moderator
Fellow 2018
Most Valuable Expert 2013

For something very basic, you want to direct all traffic to an index page where the index page is your router.  From there you can gather variables and push that to a template page.

Assume you send an array called $data to your template page that look like below.

$metaDescription = data["metaDescription"];
$title = data["title"];
$body = data["body"];
<!DOCTYPE html>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <meta name="description" content="<?php echo $metaDescription ?>"/>
  <title><?php echo $title ?></title>
<?php echo $body ?>

Open in new window

If you look at some of the "basic" php routers and start playing with them, you realize there are things missing.  To save yourself some time, give a framework a try. One place to start is and you will find it will be easier to use a template engine and there are examples using twig

While it is nice to use a framework with a seemingly small footprint, you will find using a more robust framework like Cake or Laravel really does make things easier.  For instance, cross site scripting functions are built in if you are going to be doing any forms processing.   It is worth the time to spend a day reviewing where there is some good basic php info worth looking at and OOP  While not required, they are worth the view.  There is a tutorial on building an app from scratch that will help make you feel more comfortable to using this library

If you try and roll your own or use full framework like laravel, it comes down to pointing your traffic to your index page, then routing your variables to a template page.

In regards to Wordpress, it is also a framework. If your site will need updates by users, it is worth looking into more because it is a CMS. You don't have to use plug ins for things like forms and you can process your own. It is just a matter of understanding the wordpress way of doing things

Which way you go will depend on your app.  If it is heavy on content management, then WP is worth taking a second look and learn to do custom development for WP.
for #2 the code for the title would be something like

  case 'pageX.php': echo "my nice title" ; break ;
  case 'pageY.php': echo "another title" ; break ;
  default: echo "most likely the site's name here" ;

a similar statement will deal with keywords.

a #3 alternative might be to use a couple of switch statements to populate variables in the header.php file or a separate config file before printing the headers which is easier to maintain.

a #4 alternative could be to always call the same page which deals with the headers and basic site structure with a parameter such as $page='whatever' and use that $page parameter to include the proper page, set of keywords, and title.

i would not recommend using a framework before you have experience with manually coding a simple site.
i would not recommend a framework for such a simpe task.
that said wordpress once setup will allow non power users to update the site's content quite easily, and falcon actually brings something to php. other frameworks are in my opinion mostly ways to make your site more complex to code, more complex to debug, more complex to deploy, ... and immensely slower.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial