How to add PHP to wordpress text widget

When you need, you can add to the textwidget php script. In standard version, this not work,  but after added simple code in your child theme in file: functions.php, your php code will be executed.

function uniset_php_execute($html){

    if(strpos($html,"<"."?php")!==false){ 
        ob_start(); eval("?".">".$html);
        $html=ob_get_contents();
        ob_end_clean();
    }
    return $html;
}
add_filter('widget_text','uniset_php_execute',99);

 

After that, In your textwidget add simple script, to try it. For example:

<?php
echo "Some random number: <pre>".rand()."</pre>";
?>

NOTICE: For security reasons this code is not recommended, you should not use this code on production website, but sometimes it is usable for programmers to display environment variables or for other programming needs: we can check your cache and some other reasons.

Leave a Reply

Your email address will not be published. Required fields are marked *