简体   繁体   中英

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;
}

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;
}

As RohitS comments says, you need to concenate $my_content

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

To echo the return of the et_get_footer_credits function,

echo et_get_footer_credits();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM