paul1107 paul1107 - 21 days ago 6
PHP Question

trouble with single and double quotations within a php function to output html & php

I am trying use a hook in wordpress theme to swap out footer credits on a website using a function.

As you can see the "$my_content" variable output includes php as well as html.

This string outputs as expected when used seperatly outside this particular function, so I can only deduce that the problem is caused by the added single quotation marks required by the my_content variable to output the result?

function et_get_footer_credits()
{
$my_content = '<p id="footer-info">&copy; Copyright <?php echo showDateRange('2010'); ?> <a href="<?php echo esc_url( home_url( '/' ) ); ?>">Company name</a>text</p>';
return $my_content;
}

Answer

You cannot use php in string. You should change your function, so the html part will be separate from $my_content variable. You can use ob_start and ob_get_clean function. Example:

function et_get_footer_credits() {
    ob_start();
    ?>

    <p id="footer-info">
        &copy; Copyright <?php echo showDateRange('2010'); ?> 
        <a href="<?php echo esc_url( home_url( '/' ) ); ?>">Company name</a>
        text
    </p>

    <?php
    $my_content = ob_get_clean();

    return $my_content;
}