• Resolved madleine

    (@madleine)


    Hi,
    I am adding HTML and PHP code together in widgets using HTML custom blocks but the PHP code is not rendered to the page, an example of which is below

    <p>&copy;2015- <?php echo Date('Y'); ?> Footer Area</p>

    How to add PHP code into widgets ?
    I would prefer not to use a plugin and to achieve this through coding

Viewing 3 replies - 1 through 3 (of 3 total)
  • As you’ve found, you can’t add PHP code through HTML blocks.

    To add that, you would need to create your own widget code, and add it to a child theme or a custom plugin. If you don’t have the ability to do that you really should look at a plugin that can do this as it’s really going to do what you want with pretty much the same sort of coding, just done by someone else.

    Moderator bcworkz

    (@bcworkz)

    The Custom HTML block is for HTML, not PHP ?? As you’ve discovered. You could develop your own classic widget, or a shortcode which you can add to other content, or your own custom block which can be used as a widget.

    Thread Starter madleine

    (@madleine)

    In case someone else wanted to achieve the same result as me, this is how I achieved it ??

    @assist7833 gave an answer in this thread (that is deleted now) but it worked perfectly

    in functions.php of my child theme I added the following code with few tweaks replacing “Footer Area” with the actual text I want rendered there:

    function custom_footer_year() {
        ob_start();
        echo '&copy;' . date('Y') . ' Footer Area';
        return ob_get_clean();
    }
    add_shortcode('footer_year', 'custom_footer_year');
    

    and then inside the footer widget I created, instead of the PHP code to display the current year for the copyright validity, I put this shortcode

    [footer_year]

    Thank you also to @catacaustic for the suggestions and thanks for the links @bcworkz

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to insert PHP code into widgets?’ is closed to new replies.