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.

[cc lang=”php”]
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;
}
[/cc]

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

Davinder Singh Kainth

A digital creator with 15+ years of experience in Website Design, Development, SEO, and Content Creation to Podcasting at SmartWebCreators.com with the motto of "Be Smart, Keep Creating". A coach, consultant, and your dear geek friend ❖

Leave a Comment