How To Allow PHP In The Widget Area (Without Plugin)

In a WordPress Site in the Widget Area there is a Text widget in which we can only use HTML code as it generally support HTML but not PHP code. To allow PHP in the Widget Area there are a lot of plugins. We will not take the help of a plugin but we will allow PHP in the Widget  Area with the help of some codes. The code has been given below as text and as an image.

Allow PHP in the Widget Area

Go to Edit Area of your theme by logging to your sites Dashboard or you can also do it by logging into the c panel of your WordPress site. Open the function.php file of your theme (If you are logged in Dash board) or if you are logged in c panel then go to theme section and find out the function.php file. Then copy the below code and paste it before ?>  tag. If this tag is unavailable then paste the code at the end of function.php file normally and save the file.

add_filter(‘widget_text’, ‘enable_php_code’, 99);

functionenable_php_code ($text) {

if(strpos($text, ‘<‘. ‘?’) !== false) {

ob_start();

eval(‘?’. ‘>’. $text);

$text= ob_get_contents();

ob_end_clean();

}

return$text;

}

Pre-Caution:

Before editing the function.php file keep the backup of that file. Otherwise if the code does not work then restore your backup file.

Now with this code you can allow PHP in the Widget Area. If you found this code helpful for your site then please share it and if you have any better suggestion then please let me and my visitors know by commenting in the comments section. Because I believe to learn and share anything will increase my knowledge a lot. You can also follow this motto. Explain your own idea Please. Bye Today.

Leave a Comment