Yes you can use PHP and shortcodes in widgets in WordPress. I’ve tried both of these and they work well.

  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
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;
}

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