Code Snippets

How To Allow PHP In WordPress Widget Area Without Plugin

Maan
Aug 4, 2014
1 min read
110 words

In WordPress theme, sometime you need to execute custom PHP code in widget to showcase the different information related to your category. Many plugins are available that do this, to add a new type of widget usually called PHP widget. But here you don’t need to install a plugin as it can be done by adding in functions.php file of your theme and these lines will turn by default text widget into a PHP enabled widget.

[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;
}
[/php]

Source →

Maan

Author

Maan is a Lead Frontend Engineer and Creative Director with 10+ years of industry experience blending UI/UX design with React-based development. A multifaceted practitioner, he has spent a decade turning abstract inspiration into deployed, scalable reality. Maan’s expertise lies in high-fidelity prototyping and component architecture, which he shares through insightful technical writing with the next generation of developers. His unique approach ensures that pixels not only meet passion but also rigorous performance standards.

View all posts

Continue reading

You might also like

2 responses to “How To Allow PHP In WordPress Widget Area Without Plugin”

Leave a Reply

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

Menu