Sometimes we need to execute PHP scripts in text widget but by default WordPress doesn’t comes with this feature because of security issues.

php-wordpressYou can use few plugins like PHP text widget and PHP Code Widget but why anyone should use those when we can do it within a fraction of seconds.You know what,It takes much lesser time than a installing a plugin.

FInd the functions.php file of your current theme and add the following code at the end of the file.

function php_execute($html){
if(strpos($html,"<"."?php")!==false){ ob_start(); eval("?".">".$html);
return $html;

Now clear the cache if you use Cache Plugins such as WPSuper Cache or W3 Total cache.

To check the working,Paste the following PHP code in a text widget

 <?php echo 'Hello World !!!' ?>

Now check the place where you have placed the widget,You should see “Hello World !!!


If you want to understand the code then visit – Emanuele Feronato - italian geek and PROgrammer

via – WPSquare


  1. Vincenzo says

    Hi thanks for sharing this tip it’s very useful i was wondering if there is a way to insert php into the menu as well or insert a widget in the menu

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

CommentLuv badge