Execute Php in WordPress Text Widget

Execute Php in WordPress Text Widget , Its a simple task yet We have faced a lot of headache in the past over this issue, There are a lot of occasions when having all the million widget options does not give you the exact result that you need in the website, especially for the developers. It may be a simple thing like activating a login system , or a random message based on the person who is logged into the website right now we have all faced the same issue.

I know there are a lot of plugins that promise to get you the option to write PHP codes in your text widget, and to be true its genuinely a very small thing to look for, and out of all those plugins some might be working on the current versions of the wordpress, but to find you have to go back to the Amateur level and start installing all of them and then test if its working fine or not.

Well Here is a small code that you can get rid of this issue on all theme, on any of the wordpress versions after 3.0

Add into your theme’s function.php file

// Enable PHP in widgets
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;
}

This help you achieve this cool result of adding php codes in your wordpress text widgets.

php in text widget

I sure hope this helps for all of you, Incase if you got questions or comments then feel free to get in touch via comments.

Leave a Reply

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

seventeen + 19 =