This code is used to display php inside a text widget

function execute_php($html){
return $html;

I'm currently using it in my theme. I'm constantly looking for better ways to do things. Every google search and website returned tells you to use this code or code similiar to this. With one google search I came up with this question How to allow PHP In WordPress text widget. One user came up with code similiar to this to which @Mark Kaplun commented that it will break anything that starts with <?

My question now is, I like to use the text widget to quickly test code like say something like related posts before coding it into my theme. Creating a shortcode here is unneccesary. Is there a better way to allow php in a text widget.

1 Answer 1


I think this is enough, inside the widget-function of your Widget-class.

eval( '?>' . $html );
$html = ob_get_contents();

Alternative use a maintenant plugin for this job, like PHP Code Widget

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.