I followed the code and in the file “amr-shortcode-any-widget.php” the function “amr_do_widget_area” calls the standard wordpress function dynamic_sidebar($widget_area); on line 77. When WP-Rocket plugin is deactivated it returns the full sidebar including <section> and <h2> tags. When WP-Rocket is activated it returns the sidebar without <section> and <h2> tags.
I added following code to my functions.php which checks for the “widgets_for_shortcodes” sidebar and adds those tags dynamically.
// Widget has <section> and <h2> removed somehow when WP-Rocket is active
// With this function we add them dynamically again
function widgets_for_shortcodes_widgets($params) {
// Check if we are dealing with the correct sidebar with id=”widgets_for_shortcodes”
if(isset($params[0][‘id’]) && $params[0][‘id’] == ‘widgets_for_shortcodes’){
// Get and edit parameters
$widget_id = $params[0][‘widget_id’];
$widget_number = ‘-‘.$params[1][‘number’];
$widget_class = rtrim($params[0][‘widget_id’],$widget_number);
// Only replace empty values generated by Wp-Rocket
if(empty($params[0][‘before_widget’])) {
// Since widgets have class with or without widget_xxxxx we add both
$params[0][‘before_widget’] = ‘<section id=”‘.$widget_id.'” class=”widget widget_’.$widget_class.’ ‘.$widget_class.’ “>’;
}
if(empty($params[0][‘after_widget’])) {
$params[0][‘after_widget’] = ‘</section>’;
}
if(empty($params[0][‘before_title’])) {
$params[0][‘before_title’] = ‘<h2 class=”widget-title”>’;
}
if(empty($params[0][‘after_title’])) {
$params[0][‘after_title’] = ‘</h2>’;
}
}
return $params;
}
add_filter(‘dynamic_sidebar_params’,’widgets_for_shortcodes_widgets’);
Advise for Anmari: I tested styling the [do_widget] shortcode with wrap=section and title=h2 in the shortcode. And that works fine. The [do_widget_area] doesn’t apply these parameters. With the above function you could implement that functionality to the [do_widget_area] also?
I know it is probably not your plugin’ fault Wp-Rocket doesn’t work together with the widgets_for_shortcodes sidebar. Maybe this helps others. Keep up the good work.