• Resolved hsysgrp

    (@hsysgrp)


    function my_wp_nav_menu_args( $args = '' ) {
    // Logged in menu to display;Public = 10 Menu = 9 Admin = 11
    // Public is for non-members Menu is for HFA members Admin is for Admin functions
    	if( is_user_logged_in() ) {
    
    $args['menu'] = 9;
     
    } else {
    // Non-logged-in menu to display
    
    $args['menu'] = 10;
    }
    return $args;
    }
    add_filter( 'wp_nav_menu_args', 'my_wp_nav_menu_args' );

    This code works to display different menus to logged in or non-logged viewers, but can’t figure out the endif to display the menu for admin.

    Thank you.

Viewing 9 replies - 1 through 9 (of 9 total)
  • Are you concerned with the administrator user role? A user can have multiple roles, so you need to read their list and check if “administrator” is present in it. A mini example of this can be found here:
    https://wordpress.stackexchange.com/questions/5047/how-to-check-if-a-user-is-in-a-specific-role

    Here also as a function:
    https://wpti.ps/check-if-user-has-specific-role-or-capability-in-wordpress/

    Try the current_user_can function. if current_user_can( 'activate_plugins' ) should do the trick.

    function my_wp_nav_menu_args($args = '') {
    	// Logged in menu to display;Public = 10 Menu = 9 Admin = 11
    	// Public is for non-members Menu is for HFA members Admin is for Admin functions
    
    	if ( is_user_logged_in() ) {
    
    		if ( current_user_can( 'activate_plugins' ) ) {
    			$args['menu'] = 11; // admin only menu
    		} else {
    			$args['menu'] = 9;
    		}
    
    	} else {
    		// Non-logged-in menu to display
    
    		$args['menu'] = 10;
    	}
    	return $args;
    }
    add_filter('wp_nav_menu_args', 'my_wp_nav_menu_args');

    Or shorten it to a one-line ternary; also, since the $arg is expected to be an array casting it as a string by default ($args = ”) might cause unexpected side effects.

    function my_wp_nav_menu_args($args) {
    	// Logged in menu to display; Public = 10 Menu = 9 Admin = 11
    	// Public is for non-members Menu is for HFA members Admin is for Admin functions
    
    	$args['menu'] = is_user_logged_in() ? current_user_can( 'activate_plugins' ) ? 11 : 9 : 10;
    	return $args;
    }
    add_filter('wp_nav_menu_args', 'my_wp_nav_menu_args');
    Thread Starter hsysgrp

    (@hsysgrp)

    Thank you. The “current user can activate plugins” is just the determinant I was looking for. However I get a fatal error.. my functions.php in the child is:

    <?php
    // Exit if accessed directly
    if ( !defined( 'ABSPATH' ) ) exit;
    
    // BEGIN ENQUEUE PARENT ACTION
    // AUTO GENERATED - Do not modify or remove comment markers above or below:
    
            
    if ( !function_exists( 'chld_thm_cfg_parent_css' ) ):
        function chld_thm_cfg_parent_css() {
            wp_enqueue_style( 'chld_thm_cfg_parent', trailingslashit( get_template_directory_uri() ) . 'style.css' );
        }
    endif;
    add_action( 'wp_enqueue_scripts', 'chld_thm_cfg_parent_css' );
    
    // END ENQUEUE PARENT ACTION
    
    function my_wp_nav_menu_args($args = '') {
    	// Logged in menu to display;Public = 10 Menu = 9 Admin = 11
    	// Public is for non-members Menu is for HFA members Admin is for Admin functions
    
    	if ( is_user_logged_in() ) {
    
    		if ( current_user_can( 'activate_plugins' ) ) {
    			$args['menu'] = 11; // admin only menu
    		} else {
    			$args['menu'] = 9;
    		}
    
    	} else {
    		// Non-logged-in menu to display
    
    		$args['menu'] = 10;
    	}
    	return $args;
    }
    add_filter('wp_nav_menu_args', 'my_wp_nav_menu_args');
    

    Which if any of the menus should be designated as the primary?

    Syntactically, this is correct. If there is a fatal error, check the error log. There you will find the specific cause.

    However, the use of the “activate_plugins” capability is also unfavorable from a content point of view. You asked for the role administrator. Also non-administrators could have “activate_plugins”. Therefore again my reference to my answer above.

    Thread Starter hsysgrp

    (@hsysgrp)

    Debug says syntax error, unexpected ‘}’ in /home4/hsysgrpc/public_html/hawesfamilyassociation/wp-content/themes/twentyfourteen-child/functions.php on line 46. All of the parenthesis match, I’m wondering if the code should be

    if ( is_user_logged_in() ) 
    
    		and ( current_user_can( 'activate_plugins' ) ) {

    Which PHP-version do you use?

    Thread Starter hsysgrp

    (@hsysgrp)

    Version information: 4.9.11 in BlueHost. Basically Admin is me. I want to see the Admin menu if I log in, the Menu menu if the members log in, and the Public menu if its a visitor. Bluehost if refusing to update functions.php with the code above, declaring there is an extra parenthesis. Debug.txt says the same.

    “4.9.11” sounds more like a WordPress version than a PHP version. If it’s WordPress 4.9.11, then the WordPress installation is extremely old, which is why I’m afraid that you are also using an equally old PHP version that handles certain spellings in the code differently. If my assumption is correct up to this point, I would advise you to do a general update.

    Thread Starter hsysgrp

    (@hsysgrp)

    <?php
    // functions.php in 2014 child theme 4/8/2023
    // Exit if accessed directly
    if ( !defined( 'ABSPATH' ) ) exit;
    
    // BEGIN ENQUEUE PARENT ACTION
    // AUTO GENERATED - Do not modify or remove comment markers above or below:
    
            
    if ( !function_exists( 'chld_thm_cfg_parent_css' ) ):
        function chld_thm_cfg_parent_css() {
            wp_enqueue_style( 'chld_thm_cfg_parent', trailingslashit( get_template_directory_uri() ) . 'style.css' );
        }
    endif;
    add_action( 'wp_enqueue_scripts', 'chld_thm_cfg_parent_css' );
    
    // END ENQUEUE PARENT ACTION
    
    function my_wp_nav_menu_args($args = '') {
    	// Logged in menu to display;Public = 10 Menu = 9 Admin = 11
    	// Public is for non-members Menu is for HFA members Admin is for Admin functions
    
    	if ( is_user_logged_in() ) {
    
    		if ( current_user_can( 'activate_plugins' ) ) {
    			$args['menu'] = 11; // admin only menu
    		} else {
    			$args['menu'] = 9;
    		}
    
    	} else {
    		// Non-logged-in menu to display
    
    		$args['menu'] = 10;
    	}
    	return $args;
    }
    add_filter('wp_nav_menu_args', 'my_wp_nav_menu_args');

    Success. The code above successfully presents different menus based on user category: admin, group member, and public. The first try had $args[’menu’] = 11; I’m looking at the answer above, and see the line as $args[‘menu’] = 11;

    Somewhere in copying the code from the Forum to notepad to FileZilla to theme file editor the bracket got changed to #091; syntax for the menu choices, and the php evidently choked on that. Will definitely research which version of php is being used, and watch out for substitutions in the future. Thank you for your help.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Different menu for admin’ is closed to new replies.