Site logo

Archived topic

swap sidebar through a hook

5 replies · Started by Ash on July 19, 2017

Viewing posts 1–6 of 6

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?

Yes but what I want is to assign custom sidebar to footer widget areas conditionally.

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!

This archived topic is closed to new replies.