• Resolved Doodlebee

    (@doodlebee)


    I have a quick question – at least I hope it is.

    I have this in my header.php file:

    <title><?php echo $title; ?></title>
    <meta name="description" content="<?php echo $description; ?>" />
    <meta name="keywords" content="<?php echo $keywords; ?>" />

    Now, the idea is to have my header included in all pages, but at the top of each page template, I can change the keywords, description and such by putting this at the top of each different page template:

    <?php
    $title = "extended title here";
    $description = "description here";
    $keywords = "keywords here";
    ?>

    Basically, it’s supposed to read what I’ve put at the top of each page, and insert the proper words and descriptions into the header/meta tags where it’s called in.

    However, for some reason, it’s not working with WordPress (this is something I use for sites I usually develop). Would anyone know why? Do I need to call in a particular path or something to get the header to read the included words and place them in the proper spots? Right now, they all come up blank.

    I hope that made sense to you all! Thanks 🙂

Viewing 6 replies - 1 through 6 (of 6 total)
  • Personally, I do not like mixing HTML and PHP… I would work with a “pure” PHP block and use here document to output HTML. Using <?php ?> in the middle of HTML gives me headaches at debug time, and I have already had problems like yours where PHP has “lost” the variables that you want to output!

    Here doc allows you to output HTML without having to escape anything (“, ‘, etc) but still display PHP variables. Here doc works with echo and can also be entered into a variable (<<< replacing = as an affectation operator). The instruction is followed by 3 < symbols and a unique tag. The unique tag must be closed (unique tag followed by 😉 on the first column of a new line

    The syntax is

    echo/$somthing <<< unique_tag_name
    <!– Your HTML/javascript code –>
    unique_tag_name;

    Example:
    <?php
    $title = “extended title here”;
    $description = “description here”;
    $keywords = “keywords here”;

    echo <<< here_doc_data

    <title>$title</title>
    <meta name=”description” content=”$description”>
    <meta name=”keywords” content=”$keywords”>

    here_doc_data;
    <?

    HTH,

    Daniel

    Thread Starter Doodlebee

    (@doodlebee)

    Daniel –

    thanks for the tip 🙂 And sorry for taking so long in responding – been a busy couple of months.

    I’ve tried what you suggested, and it does work – to a point. I can get it to work as you’ve stated above – but things are out of order. I don’t know if I can explain it correctly, but I’ll give it a shot.

    To Daniel (and onyone else who might have an inkling as to what’s happening!), what I want is to have the header.php file contain the variables to be called in. In other words, the header.php file contains my doctype and title, keywords, etc. tags, like any normal website does. However, I want the title, keywords and description tags to be PHP variables so that I can designate the content of those variables from each page.

    So, to do this, I have to split the above example code up. If I put the above in the header.php file, then the same title, keywords and description show up for every page on the site. This is not what I want – I want to define the variables on each page so they are all different.

    If I place the example above on my index.php file (and the other pages I want this to work on), the meta tags show up in the source code *before* the header does.

    So I’m at a loss as how to split it up properly. I had thought that having the variables *before* the get_header() command in the index file would work – the variable definitions show up before the request in the header – but it seems because they are separate files, the variables don’t recognize their definitions.

    If that’s not clear enough, basically it’s like this. My header.php file starts out like this:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

    <head>
    <title>Aneko Studios - <?php echo $title; ?></title>
    <meta name="description" content="<?php echo $description; ?>" />
    <meta name="keywords" content="<?php echo $keywords; ?>" />

    Then, in the index.php file, I place the variable definitions *above* the get_header() call, like so:

    <?php
    $title = "extended title here";
    $description = "description here";
    $keywords = "keywords here";
    get_header();
    ?>

    I would like to put the above definitions in the index.php file (to designate the “blog” area) and also on all of my Pages, to designate what Page has different keywords and such.

    So, in essence, the first thing the browser *should* see when calling up the page are the variables and their definitions – then it should call in the header of the page, and place the definitions in the proper places within the meta tags.

    But it’s not – it’s getting the header file all right – but it’s completely ignoring the variable definitions, and for the life of me, I can’t figure out why.

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    it’s completely ignoring the variable definitions, and for the life of me, I can’t figure out why.

    Because you’re calling a function (get_header) to display the header, and you’re entering a different variable scope. This is tricky to understand but in PHP, when a file is included from a function, the local scope of the function becomes the global scope of the included file. The global scope of the function itself is not accessible by the included file. So your variables are going entirely out of scope.

    Here’s an example:
    <?php
    $a = 1;
    function do_my_inc()
    {
    $b = 2;
    include ("somefile.php");
    }
    ?>

    Somefile.php looks like this;
    <?php
    echo $a;
    echo $b;
    ?>

    What do you get? Answer: 2. The reason is that while $a is accessible to the function, it’s *not* accessible to the include file. $b, being local to the function, is global to the include file.

    So, the short of it is that you can’t do this the way you want to do it. But, another way to do this would be to remove these meta tags from the header entirely, and then have each page create a function that hooks the wp_head action.

    At the end of your header.php, there should be a call to wp_head(), just before the end of the header section. This calls a wordpress function which adds a rather handy action hook. So you can do something like this:

    <?php
    function add_some_meta_flavor()
    { ?>
    <title>title</title>
    <meta name="description" content="description" />
    <meta name="keywords" content="keywords" />
    <?php }
    add_action('wp_head','add_some_meta_flavor');
    ?>

    Thread Starter Doodlebee

    (@doodlebee)

    Oh. My. God.

    This is the first I’ve heard of “hooks” – I’m now in the codex reading up on them. What amazing little things! Holy crap!

    Thanks for the explanation, Otto (I *knew* that was what the issue was – but I just can’t put stuff like that into words, you know?) – works like a charm. But now I’ve got to get my nose into these “hook” things – how useful and awesome. I swear, everytime I have some kind of issue, I find something else about WordPress that impresses me further.

    Awesome.

    (Now I’m wondering if these hooks will help out with this problem! Gotta find out!)

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    Ahh. Yes, actions and filters are key to adding stuff to wordpress.

    This is good stuff here: http://codex.wordpress.org/Plugin_API#Hooks.2C_Actions_and_Filters

    Thread Starter Doodlebee

    (@doodlebee)

    LOL – that’s the page I’m actually reading right now 😉

    Thanks again!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘PHP meta tags’ is closed to new replies.