Search Engine Optimization San Diego

Use PHP in Widgets Without a Plugin WordPress

There are times when you are working on your WordPress website and you might want to execute some php in a widget or sidebar. You can download plugins to do this but this adds bloat to your WordPress. Remember, using the least amount of plugins in your website will ensure that the site loads quickly, has limited conflicts with other plugins and is easy and quick to upgrade.

I always recommend that you do not edit your main themes’ functions.php file, rather, place a new functions.php file in a child-theme folder instead.  This way any updates to WordPress will not break your code.

wp-content/themes/your-theme-here/functions.php  (don’t touch this)

Create a new folder here:


  • notice I added -child  to the end of the new folder

so now you should have


Create a NEW functions.php file (use a text editor or notepad) and place it in this folder so it looks like this…


add this little bit of code to your theme’s function.php file:

// Enable PHP in widgets
function execute_php($html){
     return $html;


Open your WordPress Admin –> APPEARANCE —>  WIDGETS  and add whatever  php code you want to the Widget. Select a TEXT WIDGET and drag it to the SIDEBAR Widget then add your shortcode

or whatever shortcode you have.

**Hint: Remember to us <?php my code here ?> within the widget not just the code itself.

Save it. Then go to your public facing page and you should see the code working!


Of course, before making any changes, you should make a full backup of your website. If you don’t want to make a backup from your hosts control panel, you can add a plugin like UpDraft to WordPress to do this for you.

Of course, if you don’t want to do this, you can always hire us!

by James Byrne, Information Architect – After Dark Grafx