1

I am working on a Woocommerce Wordpress site.

I want to hide a widget in frontend to non-logged in users.

The widget is "filter by price" from Woocommerce. I'm also using the "Catalog Visibility Options" because I am not able to show prices to non logged-in users.

I'm using canvas theme and a child theme.

I want to use a code to insert in my custom functions.php file.

I don't want to use a plugin.

I was trying with this code, but I am not able to finish it:

// Hide Widget to non-logged user

function hide_widget() {
    if ( is_user_logged_in() ) {

    } else { 

    }
}

add_action( 'wp', 'hide_widget' );

Can somebody please assist me with this?

Thank you very much!!

4
  • @pat-j I am not able to finish this code: // Hide Widget to non-logged user function hide_widget() { if ( is_user_logged_in() ) { } else { } } add_action( 'wp', 'hide_widget' ); Thank you!
    – user45869
    Jan 30, 2014 at 1:48
  • Please edit your question to include the code. It's a lot easier to read there.
    – Pat J
    Jan 30, 2014 at 14:01
  • Is this your widget, or a widget provided by a third party-- a plugin probably?
    – s_ha_dum
    Jan 30, 2014 at 14:46
  • Thank you for your help! I have updated my main question.
    – user45869
    Jan 30, 2014 at 20:25

2 Answers 2

2

Tested on Twenty Fourteen and works.

Change the loop_start hook to another position if needed.

The code goes at the end of your child themes functions.php file.

function wpsites_register_widget() {

 register_sidebar( array(
'name' => 'Logged In Only Widget',
'id' => 'members-widget',
'before_widget' => '<div>',
'after_widget' => '</div>',
) );
}

add_action( 'widgets_init', 'wpsites_register_widget' );

add_action( 'loop_start', 'logged_in_widget', 25 );

function logged_in_widget() {


    if ( is_user_logged_in() && is_active_sidebar( 'members-widget' ) ) { 
    dynamic_sidebar('members-widget', array(
    'before' => '<div class="members-widget">',
    'after' => '</div>',
) );

      }

}

Another solution is to use a widget logic type plugin where you can add the conditional is_user_logged_in() to the existing widget.

http://codex.wordpress.org/Function_Reference/is_user_logged_in https://wordpress.stackexchange.com/a/128181/9884

1
  • Thank you, but I want to hide a widget that it is already created (filter per price by woocommerce). Do I really need to declare the plugin first? Thank you!
    – user45869
    Jan 30, 2014 at 20:27
1

FInd out where the widget is being called and wrap like this:

      if ( is_user_logged_in() ) { 
           dynamic_sidebar( 'WIDGET' );
      }
4
  • Thank you, but I don't want to edit the WP code, because of updates.
    – user45869
    Jan 30, 2014 at 1:47
  • @ArtGoddess: the suggestion is to edit the theme, which may or may not have the issues as editing WordPress core.
    – s_ha_dum
    Jan 30, 2014 at 3:58
  • Thank you, but as I'm using Canvas theme, and it is updated frequently I prefer to make the change in the child theme.
    – user45869
    Jan 30, 2014 at 20:26
  • Find where it's being called in the child theme then.
    – eteich
    Feb 4, 2014 at 22:38