Responsive WordPress Theme – Caliber

A theme with simple controls and limitless potential. Work fast and build an amazing website!   Find out more »

UpThemes – Beautiful WordPress Themes

Create a site your church, gallery, newspaper, blog, recipes, band and more!   See the themes »

Need a little help?  Find answers quickly by searching the forum.
Group Admins
  • jeffw
  • andy

Support for: Mingle – Multi-purpose WordPress Theme

Mingle (WordPress)

Public Group  |  active 7 months ago ago
Viewing post 1 to 13 (13 total posts)

(solved) Hide a menu item for non logged-in users

  • cifero

    said

    Hi,
    is there a way to hide a menu item for non logged users?

    I would like to use an optional css class in the menu editor, for example “registredonly”
    and add somewhere a line like:
    if(!is_user_logged_in()) {$css .= ‘.registredonly {display:none!important;}’.”n”;}

    is it possible in your theme? where I could add a line of code? is this line correct?
    or you suggest another way? :)

    thank you very much for support

  • greenbrook

    said

    Yes

    I did this by adding: ” function-is-user-logged-in ” as the CSS-class of the different links I wished to hide.
    I did this in the Menu-section under Appearance… Just remember to have the CSS-classes displayed in this area.

    Al right?

  • cifero

    said

    Many thanks greenbrook !!
    it works perfectly.
    yeah :)

  • rightnowloads

    said

    How can I do the opposite ? I need to hide a menu link when the user is logged in.

  • andy

    said

    You add the “-” before the conditional to make it represent “not” so…

    -function-is-user-logged-in

    This would be read aloud as “NOT user is logged in

  • rightnowloads

    said

    This is great! Thanks! if I wanted to add this to another wp buddypress them can I? is it CSS or java that is controlling the show/hide in the navigation wanted to bring the files over to another site and use the same way.

  • nitinh

    said

    what if you want to hide links based on an access level?

  • andy

    said

    @nitinh

    You can use the superuser function (http://codex.wordpress.org/Conditional_Tags#Admin_of_a_Network_.28Multisite.29) if you wanted for this:

    function-is-super-admin

    However the actual user permissions functions in WP are not true/false condition tests as needed by the menu conditionals because they need parameters and this was not included because of the complexity added. For example to test for user permissions you would use a function like:

    <?php
    # current_user_can( $capability );
    # check if current user is an 'editor'
     
    current_user_can('administrator') # => false
    current_user_can('editor') # => true
    current_user_can('contributor') # => false
    current_user_can('subscriber') # => false
    ?>

    Because there is no way to provide the $capability parameter in the conditionals you can’t use this function directly. Instead, you’ll need to define a function yourself that tests this without any parameters.

    This can be done by adding code like this to the “functions.php” file:

    <?php
    # check if current user is an 'editor'
     
    function current_user_is_editor() {
      return current_user_can('editor');
    }
    ?>

    Once that is added to “functions.php” you will be able to use the following conditional to your menu:

    Show only to editors: function-current-user-is-editor
    Show only to non-editors: -function-current-user-is-editor

  • iyke20024

    said

    Hello Andy..

    If on my case I would love to hide a menu item.. Like Register when user is logged in how do I do this.

Viewing post 1 to 13 (13 total posts)
Topic tags: css, hide, menu