How to make a single meta Description and meta Keywords in a PHP document that only has 1 header for all pages

Asked

Viewed 168 times

-1

Recently I learned how to leave a static site a little dynamic, putting the footer and header in a single document for all pages. But for each page I need a different meta Description and meta Keywords. How do I do this?

The script I used:

<?php
function carrega_pagina(){
(isset($_GET['p'])) ? $pagina = $_GET['p'] : $pagina = 'home';
if(file_exists('page_'.$pagina.'.php')):
    require_once('page_'.$pagina.'.php');
else:
    require_once('page_home.php');
endif;
}

function gera_titulos(){
(isset($_GET['p'])) ? $pagina = $_GET['p'] : $pagina = 'home';
switch ($pagina):

    case 'contato':
        $titulo = 'Contato - BizarroNEWS';
        break;

    case 'privacidade':
        $titulo = 'Privacidade | BizarroNEWS';
        break;

    case 'ultimasnoticias':
        $titulo = 'Últimas Notícias | BizarroNEWS';
        break;

    default:
        $titulo = 'BizarroNEWS | Home';
        break;

endswitch;
return $titulo;
}

Document index.php:

<?php
require_once('funcoes.php');
require_once('header.php');
carrega_pagina();
require_once('footer.php');
?>

I have 3 documents called header.php, index.php and footer.php.

The header.php only has the page header, footer.php only has the footer, and the index.php only has the PHP command that is calling the file funcoes.php (first script I pasted into question), header.php and footer.php.

And I have more documents called page_home.php, page_ultimasnoticias.php, page_contato.php and page_privacidade. Inside them only the content of the page, without footer and without header.

  • 2

    Yes, it is relevant, otherwise we will not have the slightest notion of what you have done and how to modify to meet your need.

  • @Andersoncarloswoss I updated the question.

  • @Diogenessilva how is the page being loaded in the URL? Like this? http://127.0.0.1/pagina.php

  • @Jorgematheus I’m using the Apache server. The URL looks like this: http://localhost/site/? p=home, http://localhost/site/? p=contact...etc

  • If you already have all this logic to change the title, what is the difficulty of doing exactly the same thing for the META? $titulo="contato"; $keywords="formulário, fale conosco, SAC... $description="Uma linda e aconchegante página de contato, com todos os campos e botões que você espera"

  • @Bacco That’s not quite it, I don’t know put a single META Description and Keywords for each page, being that I only have 1 header for all.

  • If you change the title, you are changing the header. Just apply the same logic to the meta tags. (the title is part of the header).

Show 2 more comments

2 answers

3


Just take what you already have, read your gera_titulos this one more for a get_titulos, I mean, it would be more interesting if the function itself generated everything, even the HTML, and honestly does not even have to separate into functions, after all will only use once

example would be something like:

  • funcoes.php (for anything you use, you don’t need the functions you created)
  • init.php (will contain what checks the pages and creates the variables)
  • header.php (will generate the goals and title)
  • footer.php (will generate footer)

init.php

<?php

$pagina = empty($_GET['p']) ? 'home' : $_GET['p'];

switch ($pagina):
    case 'contato':
        $titulo = 'Contato - BizarroNEWS';
        $keywords = 'Fale Conosco, Localização, Endereço';
        $descricao = 'Formulário de contato, mapa e telefones';
        break;

    case 'privacidade':
        $titulo = 'Privacidade | BizarroNEWS';
        $keywords = 'GDPR, Privacy, Privacidade, Proteção de dados';
        $descricao = 'Entenda seus direitos e como usamos seus dados';
        break;

    case 'ultimasnoticias':
        $titulo = 'Últimas Notícias | BizarroNEWS';
        $keywords = 'News, Newsfeed, Notícias';
        $descricao = 'Saiba tudo que acontece no Bizarro';
        break;

    default:
        $titulo = 'BizarroNEWS | Home';
        $keywords = '';
        $descricao = 'Home page';
        $pagina = 'home';
endswitch;

header.php

<html>
<head>
<title><?php echo $titulo; ?></title>
<meta name="keywords" content="<?php echo $keywords; ?>">
<meta name="description" content="<?php echo $descricao; ?>">
</head>
<body>

index php.:

<?php
require_once 'init.php';
require_once 'funcoes.php';
require_once 'header.php';
require_once 'page_' . $pagina . '.php';
require_once 'footer.php';

Of course, as you will receive everything in index.php you could reduce things well, after all they are simple things, do not need these includes all, a lot could be solved there in index.php even, for example:

index php.

<?php

$pagina = empty($_GET['p']) ? 'home' : $_GET['p'];

switch ($pagina):
    case 'contato':
        $titulo = 'Contato - BizarroNEWS';
        $keywords = 'Fale Conosco, Localização, Endereço';
        $descricao = 'Formulário de contato, mapa e telefones';
        break;

    case 'privacidade':
        $titulo = 'Privacidade | BizarroNEWS';
        $keywords = 'GDPR, Privacy, Privacidade, Proteção de dados';
        $descricao = 'Entenda seus direitos e como usamos seus dados';
        break;

    case 'ultimasnoticias':
        $titulo = 'Últimas Notícias | BizarroNEWS';
        $keywords = 'News, Newsfeed, Notícias';
        $descricao = 'Saiba tudo que acontece no Bizarro';
        break;

    default:
        $titulo = 'BizarroNEWS | Home';
        $keywords = '';
        $descricao = 'Home page';
        $pagina = 'home';
endswitch;
?><html>
<head>
<title><?php echo $titulo; ?></title>
<meta name="keywords" content="<?php echo $keywords; ?>">
<meta name="description" content="<?php echo $descricao; ?>">
</head>
<body>

<?php require_once 'page_' . $pagina . '.php'; ?>

<footer>Rodapé</footer>
</body>
</header>
  • The problem is that I don’t know how to put a single Description and keyword for each page, and I only have 1 header for all pages. I need to know how to put these META Tags on each page. And I’m going to have to do this every time, and every day I create a couple of pages for my news site. Note: The variable in the header.php document should be called $title, as it is in init.php.

  • @Bacco I saw, I can change their value using the variables, but I don’t know how to put a command that generates these meta tags only for each page. I’m the kind who learns by doing, and still doesn’t learn to do this kind of thing, I’m sorry.

  • Show! + 1 for the elaboration/creation of includes

  • 2

    @Diogenessilva I edited the answer of the colleague Guilherme above, see if it became easier to understand now the solution

  • @Bacco I got it. I only have 2 questions, I do not know if there is some kind of rule on the pages that are with the name "page_" in front of each one (I put that name pq told me), 1° I have to rename all my documents and put the "page_" in front of them? 2° Have any problem I put a title of some 100 characters in the case that is inside the switch?

  • What’s in the case has to be the same as what’s in the p= of the link used. It has to be identical. These other doubts would already be separated from what was asked (and answered).

  • @Diogenessilva 1. There are no rules, page_ is an idea, could be a folder too, or could be any other method of organization amazing 2. For PHP does not have, but in google search engine the title has limit 70 characters (if I am not mistaken), so to display in search engines do not help for something very large, descripition has limit of 160 if I am not mistaken for Google.

  • @Diogenessilva you would have to see in your original source how are the links <a href="caminho?p=nomedapagina"> and use whatever is after p=` in the case, but that’s just you looking at your source code. We wouldn’t have been able to deduce what’s right in your case, just you looking at the same source

  • @Bacco Certo, I think I’m already changing the focus of the question I asked initially, if that’s the case I create another question. Thank you!

  • @Diogenessilva remembering still that if it is very vague thing, has the network chat, I think your points already give access

Show 5 more comments

0

Attention: The example will only serve for the case cited by the author .

You can take the current page that is being used through the parameter p using $_GET['p'] and make the check according to the need, thus recording the value in a constante to be used on the pages containing the meta.

On your index.php page:

  <?php         
    $str = $_GET['p'];  
    switch($str) {
        case 'teste':
        define('TITULO',' o titulo é teste');
        break;
        case 'home':
        define('TITULO',' o titulo é home');
        break;
        default:
        define('TITULO','nenhum titulo');
        break;
    }

    include "teste2.php";
 ?>

In your file containing the metas tags, (in my case, it’s the file teste2.php) would look like this:

<?php 

    echo "Aqui está o titulo em outra pagina:: ".TITULO;
 ?>

 <meta name="description" content="<?php echo TITULO?>" />

REMARKS:

1 - I used an example URL cited by the author: http://127.0.0.1/site/? p=home

2- Note that I imported the file (include "teste2.php") at the end of the page index.php.

3- Use the DevTools from the browser to see how the tag meta stayed, in my case it’s like this: http://prntscr.com/kni7vc

  • I started studying PHP quite recently, as exactly I do this?

  • @Diogenessilva reedited the answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.