Adding PHP code to head thats generates meta tag crashes site

  • Unknown's avatar

    I am having trouble with adding PHP code to my WordPress weather directly into header.php or using functions.php. All my tests suggest it should produce a valid meta string. Either way if crashes the entire site. It is to add a meta tag for keywords using topics. I get a valid string The code is as follows using functions.php:

    function get_my_meta_keywords()
    {
    // Make sure post
    if(is_singular( ‘post’ ) == TRUE)
    {
    $ID = get_the_ID();
    // Make sure we have and ID
    if($ID != FALSE)
    {
    $meta_catagory = get_the_category($ID);
    $asize = count($meta_catagory);
    // Make sure we have at least one category
    if($asize > 0)
    {
    $category_string = “”;
    // Loop through category array
    for($i = 0; $i < $asize; $i++)
    {
    // Add to the category string
    $category_string = $category_string.$meta_catagory[$i]->name;
    // Add a comma if another category is in array
    if($i != $asize – 1)
    {
    $category_string = $category_string.’,’;
    }
    }
    // Echo the meta tag
    echo ‘<meta name=”keywords” content=”‘.$category_string;.’”>’;
    }
    }
    }
    }

    add_action( ‘wp_head’, ‘get_my_meta_keywords’);

    I have tried to add this directly in header.php in the meta tag area as simple PHP as follows:
    <?php
    // Make sure post
    if(is_singular( ‘post’ ) == TRUE)
    {
    $ID = get_the_ID();
    // Make sure we have and ID
    if($ID != FALSE)
    {
    $meta_catagory = get_the_category($ID);
    $asize = count($meta_catagory);
    // Make sure we have at least one category
    if($asize > 0)
    {
    $category_string = “”;
    // Loop through category array
    for($i = 0; $i < $asize; $i++)
    {
    // Add to the category string
    $category_string = $category_string.$meta_catagory[$i]->name;
    // Add a comma if another category is in array
    if($i != $asize – 1)
    {
    $category_string = $category_string.’,’;
    }
    }
    // Echo the meta tag
    echo ‘<meta name=”keywords” content=”‘.$category_string;.’”>’;
    }
    }
    }
    ?>

  • Hi there!

    What’s the address of the site you’re working on?

    Please note that these forums are for WordPress.com-hosted sites only. If your site is using the open-source WordPress.org software, we won’t be able to help you out, and you’ll want to seek help at these forums:

    https://wordpress.org/support/forums/.

  • The topic ‘Adding PHP code to head thats generates meta tag crashes site’ is closed to new replies.