Add PHP and Shortcodes in Widgets in WordPress

Yes you can use PHP to run shortcodes in widgets in WordPress. I’ve tried both of these methods to run the code in widgets and they work well with some quick adjustments to your child theme’s functions.php file.

  1. Use shortcodes in widgets without a plugin
  2. Execute PHP code in widgets without a plugin.

Use Shortcodes in Widgets Without a Plugin

Add this code to your functions.php file in your child theme.

// Enable shortcodes in widgets
add_filter('widget_text', 'do_shortcode');

Use PHP in Widgets Without a Plugin

To use PHP in widgets without add this code to your functions.php:

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

Please note this security warning from Carrie Dils: Use PHP in widgets very judiciously as this is a potential security vulnerability you open by enabling code execution from a widget, versus a file on the server.

For Divi Theme users you can also use the above to run PHP in a module. After adding the code above to your functions.php file, create a new sidebar, add a text widget with the PHP code and then use the sidebar module in Divi Builder.

Source: Widgets Gone Wild

Posted in

Mr. Dif

Describing the difference between your available options. Here to help you where I can with your WordPress and digital problems.