Execute PHP code in WordPress text widget box

If you use PHP code in standard text widget box in WordPress, there will be no output. This is because, text widgets do not support execution of PHP codes. However, there are ways to enable text widget box to run and execute PHP code seamlessly.

1. Use ‘PHP Code Widget’ WordPress plugin

Just install PHP Code Widget plugin in your WordPress blog. Once you activate this plugin, goto Appearance > Widgets section.

You should see new widget ‘PHP code’. Use this widget to for any arbitrary text, HTML or PHP Code – it should work like charm.

2. Execute PHP code in text widget without plugin

To love using manual code instead of WordPress plugins, then add following code to functions.php file to enable text widget to execute PHP code on your WordPress blog.

1
2
3
4
5
6
7
8
9
10
11
add_filter('widget_text','execute_php',100);
function execute_php($html){
     if(strpos($html,"<"."?php")!==false)
     {
          ob_start();
          eval("?".">".$html);
          $html=ob_get_contents();
          ob_end_clean();
     }
     return $html;
}

In case above code does not work, you can always use ‘PHP code widget’ plugin for adding PHP execution funtionality on your WordPress blog.

SHARE

Speak Your Mind

*

Subscribe to our mailing list

* indicates required
Close