September 7, 2018 292 Views 0 Comment

Enable PHP In WordPress Widgets


The default Text Widget in WordPress allows you to insert arbitrary Text and/or HTML/CSS code but how does one

Enable PHP In WordPress Widgets..?

First of all enabling PHP code to execute from inside widgets can save us plenty of time to do tasks that would usually involve connecting to the backend. If access to the filesystem is an issue for you this nifty trick will be a life saver.

The code below allows WordPress to parse any PHP code in the text widget and executes it. All PHP code must first be enclosed in the standard php opening and closing tags ( ) for it to be recognized and executed however.

Only users with the unfiltered_html role will be allowed to insert unfiltered HTML. This includes PHP code, so users without admin or editor permissions will not be able to use this method to embed and execute php code, even if they have widget editing permissions.

// allow php in wp widgets
function execute_php($html){
     return $html;

Once you insert the above code inside of your functions.php file located in the themes folder, you can start embedding php snippets inside of your widgets. An alternative to this is installing a plugin called PHP Code Widget.