Author Topic: Wordpress Menu Help  (Read 1332 times)

Valiant

  • aka Sam
    • Radiance Audio
Wordpress Menu Help
« on: 09 February, 2018, 01:39:39 am »
I'm trying to make it so that a particular user role is shown a particular menu instead of the standard one on my site.


I want all users to see "Main Menu" as the primary menu. As they do now. But I want "wholesale_customer" to see "Wholesale" menu instead.


Here are the current registered menus

Code: [Select]
register_nav_menu('primary', __('Primary Menu', 'oxy'));
register_nav_menu('topnavhor', __('Top Horizontal Menu', 'oxy'));
register_nav_menu('topnav', __('Top Dropdown Menu', 'oxy'));

Here is the code I have

Code: [Select]
// Change main menu for Wholesale Customers
function my_wp_nav_menu_args( $args = ” ) {
if ($args[‘theme_location’] == ‘primary’) {
if( is_user_wholesale_customer()) {
$args[‘menu’] = ‘Wholesale’;
}else{
$args[‘menu’] = ‘Main Menu’;
}
}
return $args;
}
add_filter( ‘wp_nav_menu_args’, ‘my_wp_nav_menu_args’ );
What's wrong with the code as I keep getting a Error 500 when add it to my child theme's functions.php file.
You have the right to remain silent. Anything you say will be misquoted, then used against you.

Support Equilibrium

Valiant

  • aka Sam
    • Radiance Audio
Re: Wordpress Menu Help
« Reply #1 on: 11 February, 2018, 05:35:15 am »
Ok so these might better a better way to do it, but they both give me a 500 error.


Code: [Select]
/* Menu swap out for Wholesale */


$user = wp_get_current_user();


if ( in_array( 'wholesale_customer', (array) $user->roles ) {
    wp_nav_menu( array( 'primary' => 'Wholesale' ) );


} else  {
     wp_nav_menu( array( 'primary' => 'Main Menu' ) );


or this

Code: [Select]
       global $current_user;


        get_currentuserinfo();


        switch (true)  {
       
         case ( user_can( $current_user, "wholesale_customer") ):?>
           <?php wp_nav_menu( array( 'primary' => 'Wholesale' ) ); 
         break
         case ( 
user_can$current_user"customer") )?>

           <?php wp_nav_menu( array( 'primary' => 'Main Menu' ) ); 
         break;
          default:
          
wp_nav_menu( array( 'primary' => 'Main Menu' ) );
          break;
        }
You have the right to remain silent. Anything you say will be misquoted, then used against you.

Support Equilibrium

Re: Wordpress Menu Help
« Reply #2 on: 11 February, 2018, 08:57:04 am »
What's in the server logs?

Your first version has smart quotes in it, if those are in the actual file it's a syntax error.
Quote from: tiermat
that's not science, it's semantics.

Valiant

  • aka Sam
    • Radiance Audio
Re: Wordpress Menu Help
« Reply #3 on: 11 February, 2018, 09:41:39 am »
I can't find anything in the error logs.
You have the right to remain silent. Anything you say will be misquoted, then used against you.

Support Equilibrium

Re: Wordpress Menu Help
« Reply #4 on: 11 February, 2018, 09:56:15 am »
A 500 will have some output, somewhere. Have you tried the suggestions at the bottom of https://stackoverflow.com/questions/7576759/how-to-trace-what-is-causing-wordpress-error-500
Quote from: tiermat
that's not science, it's semantics.

Valiant

  • aka Sam
    • Radiance Audio
Re: Wordpress Menu Help
« Reply #5 on: 11 February, 2018, 10:14:02 am »
Ah got the debug. I forgot that was turnt off my default.


Code: [Select]
[11-Feb-2018 10:09:35 UTC] PHP Parse error:  syntax error, unexpected 'case' (T_CASE) in ../functions.php on line 31
[11-Feb-2018 10:09:44 UTC] PHP Parse error:  syntax error, unexpected 'case' (T_CASE) in ../functions.php on line 31
[11-Feb-2018 10:09:46 UTC] PHP Parse error:  syntax error, unexpected 'case' (T_CASE) in ../functions.php on line 31


Which refers to
Code: [Select]
         case ( user_can( $current_user, "customer") )?>
You have the right to remain silent. Anything you say will be misquoted, then used against you.

Support Equilibrium

Vernon

  • zzzZZZzzz
Re: Wordpress Menu Help
« Reply #6 on: 11 February, 2018, 10:24:31 am »
Not my language, so not sure of the syntax, but:
The if statement I'm code1 is missing a closing brace.
The second case statement in code2 is missing a colon (before the question mark).

Re: Wordpress Menu Help
« Reply #7 on: 11 February, 2018, 01:30:13 pm »
And the cause of the message is a missing semicolon on line 30.
Quote from: tiermat
that's not science, it's semantics.

Re: Wordpress Menu Help
« Reply #8 on: 11 February, 2018, 02:12:50 pm »
I know this is a Wordpress thread, but I just wanted to say that after so may years with Wordpress and its endemic failings - for me anyway - I've given up and changed to WIX instead. So much easier for a non-HTML'r like me.

Valiant

  • aka Sam
    • Radiance Audio
Re: Wordpress Menu Help
« Reply #9 on: 11 February, 2018, 07:30:20 pm »
Hey thanks for this. Eventually this worked:

Code: [Select]
        global $current_user;
get_currentuserinfo();
switch (true) {
case ( user_can( $current_user, "wholesale_customer") ):
wp_nav_menu( array( 'primary' => 'Wholesale' ) );
break;
case ( user_can( $current_user, "Customer") ):
wp_nav_menu( array( 'primary' => 'Main Menu' ) );
break;
default:
wp_nav_menu( array( 'primary' => 'Main Menu' ) );
break;
}

However now getting errors with the header etc.

PHP Warning:  Cannot modify header information - headers already sent by (output started at .../wp-includes/nav-menu-template.php:256) in .../wp-includes/pluggable.php on line 1216

As well as various other warnings etc.
You have the right to remain silent. Anything you say will be misquoted, then used against you.

Support Equilibrium

Valiant

  • aka Sam
    • Radiance Audio
Re: Wordpress Menu Help
« Reply #10 on: 11 February, 2018, 08:53:17 pm »
Argh after spending countless hours on this, I then find this https://codecanyon.net/item/menu-by-user-role-for-wordpress/3127921

Bastids.
You have the right to remain silent. Anything you say will be misquoted, then used against you.

Support Equilibrium