[Resolved] swap sidebar through a hook

Home Forums Support [Resolved] swap sidebar through a hook

Home Forums Support swap sidebar through a hook

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #351952
    Ash

    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?

    #352056
    Tom
    Lead Developer
    Lead Developer
    #352078
    Ash

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

    #352305
    Tom
    Lead Developer
    Lead Developer

    Perhaps a plugin like this will help then: https://en-ca.wordpress.org/plugins/content-aware-sidebars/

    #352432
    Ash

    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');
    #352441
    Tom
    Lead Developer
    Lead Developer

    Interesting, thanks for sharing!

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.