• Resolved Steeven

    (@steeven)


    Hi. I have added the follow piece of code to the functions.php file. This adds a specific button to the main menu if the user is logged in, and another if he isn’t.

    // Change menu button when logged in
    add_filter('wp_nav_menu_main-menu_items', 'menu_add_admin_buttons', 20, 2); 
    
    function menu_add_admin_buttons( $items, $args ) {
      $btn_format = '<li class="menu-item menu-item-type-post_type menu-item-object-page"><a href="%s">%s</a></li>';
      if ( is_user_logged_in() ) {
        $btn = sprintf($btn_format, 'https://url1', __('Your profile') );
      } else {
        $btn = sprintf($btn_format, 'https://url2', __('Log In') );
      }
      return $items . $btn;
    }

    How can I alter this a bit or copy it with changes, so it adds a SUBMENU button to one of my menu buttons, only if the logged in user is an ADMIN?

Viewing 1 replies (of 1 total)
  • Thread Starter Steeven

    (@steeven)

    Solved. This is a method:

    // Change menu button when logged in
    add_filter('wp_nav_menu_main-menu_items', 'menu_add_admin_buttons', 20, 2); 
    
    function menu_add_admin_buttons( $items, $args ) {
    	$btn_format = '<li class="menu-item menu-item-type-post_type menu-item-object-page"><a href="%s">%s</a>%s</li>';
    
    	$admin_btns_format = '<ul class="sub-menu">';
    	$admin_btns_format .= '<li class="menu-item menu-item-type-post_type menu-item-object-page"><a href="https://url3">Submenu button</a></li>';
    	$admin_btns_format .= '</ul>';
    
    	// If logged in
    	if ( is_user_logged_in() ) {
    
    		// If admin
    		if(current_user_can('has_admin_rights')) // I have added this one my self. You can use fx 'edit_pages' instead
    			$submenu_btns = $admin_btns_format;
    		else
    			$submenu_btns = '';
    
    		$btn = sprintf($btn_format, 'https://url1', __('Your profile'), __($submenu_btns) );
    
    	// If not logged in
    	} else {
    		$btn = sprintf($btn_format, 'https://url2', __('Log In'), '' );
    	}
    	return $items . $btn;
    }
Viewing 1 replies (of 1 total)
  • The topic ‘WP Explorers Adapt theme: Add a submenu button’ is closed to new replies.