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!!

closed as unclear what you're asking by Nicolai, markratledge, tfrommen, Wyck, t31os Jan 30 '14 at 8:18

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • @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! – ArtGoddess Jan 30 '14 at 1:48
  • Please edit your question to include the code. It's a lot easier to read there. – Pat J Jan 30 '14 at 14:01
  • Is this your widget, or a widget provided by a third party-- a plugin probably? – s_ha_dum Jan 30 '14 at 14:46
  • Thank you for your help! I have updated my main question. – ArtGoddess Jan 30 '14 at 20:25
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

  • 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! – ArtGoddess Jan 30 '14 at 20:27
1

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

      if ( is_user_logged_in() ) { 
           dynamic_sidebar( 'WIDGET' );
      }
  • Thank you, but I don't want to edit the WP code, because of updates. – ArtGoddess Jan 30 '14 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 '14 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. – ArtGoddess Jan 30 '14 at 20:26
  • Find where it's being called in the child theme then. – eteich Feb 4 '14 at 22:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.