Use PHP in Widgets Without a Plugin WordPress January 19, 2018at 06:53 am By: James Byrne After Dark Grafx 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: wp-content/themes/your-theme-here-child/ notice I added -child to the end of the new folder so now you should have wp-content/themes/your-theme-here/functions.php wp-content/themes/your-theme-here-child/ Create a NEW functions.php file (use a text editor or notepad) and place it in this folder so it looks like this… wp-content/themes/your-theme-here-child/functions.php add this little bit of code to your theme’s function.php file: // Enable PHP in widgets 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; } Next… 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! FIN! 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