Archived topic
swap sidebar through a hook
5 replies · Started by Ash on July 19, 2017
Related to previous my topic.
I also want to swap footer sidebars depend on cookie.
But this can't be done with sidebar controlling plugin.
How do I swap sidebar through a hook with GP?
Have you checked out this filter?: https://docs.generatepress.com/article/generate_sidebar_layout/
Some examples: https://docs.generatepress.com/article/sidebar-layout/#using-a-function
Yes but what I want is to assign custom sidebar to footer widget areas conditionally.
Perhaps a plugin like this will help then: https://en-ca.wordpress.org/plugins/content-aware-sidebars/
No plugin can handle this particular condition.
But apparently dynamic_sidebar_params hook does the trick.
function my_sidebar_params($params){
if(preg_match("/footer-([0-9])/",$params[0]['id'],$rgx) && !empty($_COOKIE['translator-translator-jquery-to'])){
$sdid='footer-'.$rgx[1].'-2';
global $wp_registered_sidebars,$wp_registered_widgets;
$sidebar = $wp_registered_sidebars[$sdid];
$sidebars_widgets = wp_get_sidebars_widgets();
foreach((array)$sidebars_widgets[$sdid] as $id){
if(!isset($wp_registered_widgets[$id])) continue;
$params = array_merge( array( array_merge( $sidebar, array('widget_id' => $id, 'widget_name' => $wp_registered_widgets[$id]['name']) ) ), (array) $wp_registered_widgets[$id]['params'] );
$classname_ = '';
foreach((array) $wp_registered_widgets[$id]['classname'] as $cn){
if(is_string($cn)) $classname_ .= '_' . $cn;
elseif (is_object($cn)) $classname_ .= '_' . get_class($cn);
} $classname_ = ltrim($classname_, '_');
$params[0]['before_widget'] = sprintf($params[0]['before_widget'], $id, $classname_);
}
}
return $params;
}
add_filter('dynamic_sidebar_params','my_sidebar_params');
Interesting, thanks for sharing!