Hide menu to not logged users

resolved (8 posts) (4 voices)

  1. nec
    Member

    Hi,

    I am trying to use this tips (http://www.billerickson.net/customizing-wordpress-menus/#logged-in) to hide a menu from not logged users but it seems not to work, do you have a css class that is made for this purpose ?
    Otherwise, do you have an idea why the provided code does work when I add it to my style.css

    thx

    Posted 2 years ago #
  2. Elliott
    Support

    Hello nec,

    We already have a menu walker which you can find in /wp-content/themes/[your_theme]/lib/classes/menu-walker.php. You probably need to add the classes there instead of adding your own in the functions.php file.

    Posted 2 years ago #
  3. nec
    Member

    The method in the first post should avoid me to modify any php file,
    I was talking about css class not php class :)

    I have activated the CSS Classes for my menus
    I have added the class "logged-in-nav" to the menu I want it to be displayed only for logged users
    I have added this ▼ to /wp-content/themes/dejavu/style.css

    #nav li.logged-in-nav {
    display: none;
    }

    body.logged-in #nav li.logged-in-nav {
    display: list-item;
    }

    but it doesn't work.

    Posted 2 years ago #
  4. Elliott
    Support

    That's going to have to be considered custom work. There are lots of membership plugins out there that you can try out though.

    Posted 2 years ago #
  5. nec
    Member

    Ok after a long search here is what I have done, it is almost working but I got a little problem remaining

    - I have activated the CSS Classes for my menus
    - I have added the class "logged-in-nav" to the menu I want it to be displayed only for logged users
    - I have created and activated a child theme for dejavu (dejavu-child)
    - I have added this ▼ to a child theme /wp-content/themes/dejavu-child/style.css

    /*
    Theme Name: Dejavu-Child
    Theme URI: http://mysitemyway.com/
    Description: Child Theme for DejaVu From A Mysitemyway Premium Wordpress Theme
    Author: Mysitemyway
    Author URI: http://mysitemyway.com/
    Template: dejavu
    Version: 1.3
    */
    
    @import url("../dejavu/style.css");
    
    li.logged-in-nav
    {
    	display: none;
    }
    
    body.logged-in li.logged-in-nav
    {
    	display: list-item;
    }

    - I have added this ▼ to /wp-content/themes/dejavu-child/function.php

    <?php
    // Add specific CSS class by filter
    add_filter('body_class','nec_logged_in_filter');
     
    function nec_logged_in_filter($classes)
    {
    	if( is_user_logged_in() )
    	{
    		$classes[] = 'logged-in';
    	}
     
    	// return the $classes array
    	return $classes;
    }
    ?>

    when I add the CSS class 'logged-in' manually to the body class attribute I can activate or deactivate menus with the "logged-in-nav" class but the problem here is that the nec_logged_in_filter function doesn't seems to be called by the body_class filter, or if it is, it doesn't add 'logged-in' to classes array, or if it is then it is cleared later. :/

    Posted 2 years ago #
  6. nec
    Member

    Ok I finally solved my last problem, in fact body call mysite_body_class() in content.php
    so I have copied the mysite_body_class function to my child theme and add it the code in nec_logged_in_filter.

    Posted 2 years ago #
  7. thekatzcorner
    Member

    Hey there, I also found this plugin for wordpress that works like a charm: http://www.kathyisawesome.com/449/nav-menu-roles/

    I needed to do the same thing, hide a menu from logged out user and I found this post, but then searching around a bit more and I found that wordpress plugin. :) Hope that helps!

    Posted 2 years ago #
  8. Thanks a lot for sharing this!

    And let us know if you need anything else!

    []'s

    Posted 2 years ago #

Reply

You must log in to post.

Construct WordPress Theme
Construct wordpress theme
Myriad WordPress Theme
Myriad wordpress theme
Method WordPress Theme
Method wordpress theme
Fusion WordPress Theme
Fusion wordpress theme
Elegance WordPress Theme
Elegance wordpress theme
Echelon WordPress Theme
Echelon wordpress theme
Dejavu WordPress Theme
Dejavu wordpress theme
Modular WordPress Theme
Modular wordpress theme