Support » Plugin: Nav Menu Roles » Add CSS classes

  • Resolved tajin

    (@tajin)


    First off, great work on that plugin.

    I think it would be useful if the plugin added some CSS classes to each menu-item, based on the settings for it.

    That would rather easily allow us to style those menu-items differently and make it more user-friendly.

Viewing 1 replies (of 1 total)
  • Plugin Author helgatheviking

    (@helgatheviking)

    Hi! Thanks for the suggestion. It’s a decent idea, though I am very hesitant to add anything not immediately related to the plugin’s mission of hiding/showing menu items. It’s possible to do on your own. I just wrote this snippet, which should get you started:

    
    
    /**
     * Add Nav Menu Role info to menu classes.
     * @param  obj $nav_item The nav menu item object.
     * @return obj
     */
    function kia_nmr_role_classes( $nav_item ) {
    	if( isset( $nav_item->roles ) ){
    		
    		$new_classes = array();
    
    		switch( $nav_item->roles ) {
    			case 'in' :
    				$new_classes[] = 'nmr-logged-in';
    				break;
    			case 'out' :
    				$new_classes[] = 'nmr-logged-out';
    				break;
    			default:
    				if ( is_array( $item->roles ) && ! empty( $item->roles ) ) {
    					foreach ( $item->roles as $role ) {
    						$new_classes[] = 'nmr-' . $role;
    					}
    				}
    				break;
    		}
    
    		$nav_item->classes = array_merge( $nav_item->classes, $new_classes );
    	}
    	return $nav_item;
    }
    add_filter( 'wp_setup_nav_menu_item', 'kia_nmr_role_classes', 20 );
    

    nmr-logged-in is added on items that you’ve set to be shown to logged in users only, nmr-logged-out for logged out users, and nmr-$role ex: nmr-admin for each role restriction.

Viewing 1 replies (of 1 total)
  • The topic ‘Add CSS classes’ is closed to new replies.