skip to Main Content

There are two files that I am able to modify

header.tpl and product.tpl

I do not have access to any of the controller or model files

For SEO purposes I am trying to modifiy the following meta data while on a product page.

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

Currently $description does not have any output. However on my product.tpl I have a variable <?php echo $heading_title ?> which is essentially the text that I would like to have in the header meta data.

Is this even possible to achieve without accessing the model/controller or am I just wasting my time?

2

Answers


  1. Chosen as BEST ANSWER

    The not so pretty but functional solution

    <?php echo $header = preg_replace('/<meta name="description" content="" />/', '<meta name="description" content="' . $heading_title . '" />', $header); ?>
    

  2. As noted in the comments: Once something has been sent to the browser (or any other output), it is impossible to change it server-side.

    The best solution, if you are able to edit the contents of the header with preg_replace() etc, is to simply set the variable before including the header.
    So that instead of something like this:

    include "header.tpl";
    $data = get_from_db ();
    echo do_some_processing ($data);
    include "footer.tpl";
    

    You move all of the processing before you start to output anything to the browser, which completely eliminates the paradox of having to change something you’ve already sent. Which should leave you with a code looking something like this:

    // Do all of the processing first.
    $data = get_form_db ();
    $output = do_some_processing ();
    
    // Then, when all of the processing is done, output to browser.
    include "header.tpl";
    echo $output;
    include "footer.tpl";
    

    It also has the added benefit of making your PHP code much simpler to read, and thus easier to maintain in the long run. Even on this small code sample, you can see the difference a proper seperation of concerns does.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search