Discrete menus for public and logged-in users/members?
Topics related to RedWaves lite theme goes here.

dnuttall

Member Rank
Posts: 18
Joined: Thu Sep 24, 2015 10:30 am

Discrete menus for public and logged-in users/members?

linkPost by dnuttall » Tue Aug 21, 2018 9:24 am

I've been using RedWaves lite for some time now and have decided to expand it with a couple of private user groups which will be managed by HQ Tips & Tricks "eMember" plugin. I use an option where new users become WordPress users as well as "eMember" users. WordPress sees them as "subscribers".

Around 6 years ago, the eMember folks posted this as general instructions for modifying one of the standard WordPress themes, but of course many things have changed in that period.
Here's their instructions:

https://www.tipsandtricks-hq.com/wordpr ... embers-551

I think I can "adapt" RedWaves Lite to achieve menus for logged-in vs. the public, but am not comfortable that there might not be elements in current themes that didn't exist 6 years ago.

If NECESSARY, I'm more than willing to upgrade to the Pro RedWaves, especially if it would be easier to tweak the child theme, etc.

Will provide active URL for the site that I want to change.
TIA.
Dave
San Antonio, Texas
User avatar
Asmi Khalil

Site Admin Rank
Posts: 781
Joined: Fri Jun 26, 2015 7:13 am
Display Name: Asmi Khalil
Location: Tunisia

Re: Discrete menus for public and logged-in users/members?

linkPost by admin » Tue Aug 21, 2018 8:55 pm

Hello Dave,
Nice to hear from you again :D I am glad you still using RedWaves lite

Currently, It's easier to tweak RedWaves than RedWaves lite but the later will receive an update soon including Bootstrap support. You should consider upgrading to RedWaves only if you need all (or at least most of) the bells and whistles that RedWaves provide (it's more suitable for magazines and news websites).

Also, the speed of RedWaves lite is not possible using RedWaves just because features come at a price even with all the optimizations I've done.

In your case, we just need to wrap the primary menu code with a PHP if statement that checks whether the user is logged in or not and display the right menu accordingly.

So, you just need to edit header.php file and replace this code (line 42):
<?php if ( has_nav_menu( 'primary' ) ) {
	wp_nav_menu( array( 'theme_location' => 'primary', 'menu_class' => 'menu' ) ); 
}else{ ?>
	<div class="no-menu-msg"><?php _e('Please assign a menu (Go to Appearance => Menus and assign a menu to "Primary Menu" location)', 'redwaves-lite') ?></div>
<?php } ?>
With this one:
<?php if ( is_user_logged_in() ) {
	wp_nav_menu( array( 'theme_location' => 'logged-users-menu', 'menu_class' => 'menu' ) ); 
} else {
	wp_nav_menu( array( 'theme_location' => 'primary', 'menu_class' => 'menu' ) );
} ?>
And replace logged-users-menu with the menu ID for logged in users.

If you have a menu for each eMember subscriber level, you can expand the code as follow:
<?php if ( is_user_logged_in() && function_exists( 'wp_emember_is_member_logged_in' ) ) {

	if ( wp_emember_is_member_logged_in( '1' ) ) {//Show this menu to members of membership level 1
		wp_nav_menu( array( 'theme_location' => 'level-1-menu', 'menu_class' => 'menu' ) );
	} else if ( wp_emember_is_member_logged_in( '2' ) ) {//Show this menu to members of membership level 2
		wp_nav_menu( array( 'theme_location' => 'level-2-menu', 'menu_class' => 'menu' ) );
	} else if ( wp_emember_is_member_logged_in( '3' ) ) {//Show this menu to members of membership level 3
		wp_nav_menu( array( 'theme_location' => 'level-3-menu', 'menu_class' => 'menu' ) );
	}
} else {
	wp_nav_menu( array( 'theme_location' => 'primary', 'menu_class' => 'menu' ) );
} ?>
And you can add add additional "else if" statements for as many as you need.
We also check if wp_emember_is_member_logged_in exists before executing it because it is not a WP code functions. Without it, you will get an error when you deactivate eMember plugin or the function name changed for some reasons.

If you are having difficulties implementing this code, give me all your groups IDs and I will customize it for you.
I think there is a world market for maybe five computers. - IBM chairman Thomas Watson, 1943
dnuttall

Member Rank
Posts: 18
Joined: Thu Sep 24, 2015 10:30 am

Re: Discrete menus for public and logged-in users/members?

linkPost by dnuttall » Tue Aug 21, 2018 9:46 pm

I will give that a good effort and expect that I can make it work!
THANKS and best regards.
d.
dnuttall

Member Rank
Posts: 18
Joined: Thu Sep 24, 2015 10:30 am

Re: Discrete menus for public and logged-in users/members?

linkPost by dnuttall » Wed Aug 22, 2018 9:04 am

I guess I am confused about how to find (or perhaps DESIGNATE is more specific!) the "menu ID" for logged in users.

When I edit the new menu that I want for logged-in-users, it shows ID=278 and the primary/main as ID=35.

Sorry to be such a "techno-wuss" (not sure if that is a word, but....!)
Thanks.
Dave
User avatar
Asmi Khalil

Site Admin Rank
Posts: 781
Joined: Fri Jun 26, 2015 7:13 am
Display Name: Asmi Khalil
Location: Tunisia

Re: Discrete menus for public and logged-in users/members?

linkPost by admin » Wed Aug 22, 2018 8:13 pm

Oops! My bad :oops: Since we are referring to the menu by ID, the theme_location parameter must be replaced with menu which accepts an ID, slug or Name. The name is whatever you enter in the Menu Name text box.

So, the code should look like this:
<?php if ( is_user_logged_in() ) {
	wp_nav_menu( array( 'menu' => '278', 'menu_class' => 'menu' ) ); 
} else {
	wp_nav_menu( array( 'menu' => '35', 'menu_class' => 'menu' ) );
} ?>
Or even like this:
<?php if ( is_user_logged_in() && function_exists( 'wp_emember_is_member_logged_in' ) ) {

	if ( wp_emember_is_member_logged_in( '1' ) ) {//Show this menu to members of membership level 1
		wp_nav_menu( array( 'menu' => '10', 'menu_class' => 'menu' ) );
	} else if ( wp_emember_is_member_logged_in( '2' ) ) {//Show this menu to members of membership level 2
		wp_nav_menu( array( 'menu' => '11', 'menu_class' => 'menu' ) );
	} else if ( wp_emember_is_member_logged_in( '3' ) ) {//Show this menu to members of membership level 3
		wp_nav_menu( array( 'menu' => '12', 'menu_class' => 'menu' ) );
	}
} else {
	wp_nav_menu( array( 'menu' => '35', 'menu_class' => 'menu' ) );
} ?>
Let me know if it works..
I think there is a world market for maybe five computers. - IBM chairman Thomas Watson, 1943
dnuttall

Member Rank
Posts: 18
Joined: Thu Sep 24, 2015 10:30 am

Re: Discrete menus for public and logged-in users/members?

linkPost by dnuttall » Wed Aug 22, 2018 8:55 pm

It works fine if I use the second fragment.
Apparently it NEEDS to know that the user is logged in to eMember as well as WordPress.
But that is the exact result that I want!

MANY THANKS!
d.
User avatar
Asmi Khalil

Site Admin Rank
Posts: 781
Joined: Fri Jun 26, 2015 7:13 am
Display Name: Asmi Khalil
Location: Tunisia

Re: Discrete menus for public and logged-in users/members?

linkPost by admin » Fri Aug 24, 2018 8:19 pm

Most welcome :D

So a logged in user using the default WP login form doesn't make it "logged in" in the eyes of eMember? I thought it's the same. Well, that explains why eMember has its own function to check if the user is logged in.

In this case, it's better to slightly modify the code as follow:
<?php if ( function_exists( 'wp_emember_is_member_logged_in' ) && wp_emember_is_member_logged_in() ) {

	if ( wp_emember_is_member_logged_in( '1' ) ) {//Show this menu to members of membership level 1
		wp_nav_menu( array( 'menu' => '10', 'menu_class' => 'menu' ) );
	} else if ( wp_emember_is_member_logged_in( '2' ) ) {//Show this menu to members of membership level 2
		wp_nav_menu( array( 'menu' => '11', 'menu_class' => 'menu' ) );
	} else if ( wp_emember_is_member_logged_in( '3' ) ) {//Show this menu to members of membership level 3
		wp_nav_menu( array( 'menu' => '12', 'menu_class' => 'menu' ) );
	}
} else {
	wp_nav_menu( array( 'menu' => '35', 'menu_class' => 'menu' ) );
} ?>
By using the wp_emember_is_member_logged_in() function without passing any parameters, this will check if the user is logged into eMember and thus, eliminate the possibility of the user doesn't get any menus displayed because he is (somehow) logged into WordPress and not into eMember (our previous code tells WordPress to display nothing in this particular situation).

Also, since PHP has Short-circuit evaluation (https://en.wikipedia.org/wiki/Short-circuit_evaluation), it doesn't execute the argument wp_emember_is_member_logged_in() and throw an error if eMember is deactivated because in this case, function_exists( 'wp_emember_is_member_logged_in' ) will evaluate to False which is enough to evaluate the whole check to False. In other words, if you ever deactivate eMember, the theme will continue to work normally.
I think there is a world market for maybe five computers. - IBM chairman Thomas Watson, 1943
dnuttall

Member Rank
Posts: 18
Joined: Thu Sep 24, 2015 10:30 am

Re: Discrete menus for public and logged-in users/members?

linkPost by dnuttall » Wed Sep 19, 2018 7:57 pm

admin wrote:
Fri Aug 24, 2018 8:19 pm
So a logged in user using the default WP login form doesn't make it "logged in" in the eyes of eMember? I thought it's the same. Well, that explains why eMember has its own function to check if the user is logged in.

I didn't think to mention earlier that eMember installs/default configuration as a PARALLEL to WP login, but it also can be configured so that when a user is created in eMember, a parallel wp_users record gets created. Their profile edit will correctly change the password in both wp_users and the eMember_users.

And it also has its own membership "levels".

I set mine up linked to the WP "subscriber" level.
eMember allows a user to be a member of more than one level, but I have discovered trying to do discrete menus (as in the foregoing code on this thread), that a login by a member authorized at two levels seems to always pick the lowest level and no clear way to stipulate the higher level if authorized.

So the only practical workaround is to NOT endow members with anything but the highest level you want them to operate at and be sure the content that you're providing is allowed for all logged in levels.

It wouldn't surprise me if there is some "tip" in the Tips & Tricks HQ forum that helps work thru this issue, but their forum search is really very difficult to find anything and unless you pay an annual fee, you can't post to their support forum, nor will they answer specific questions. At least they don't require an annual fee to get UPDATES, which was one of the biggest "features" for me several years ago when I purchased the license.

But no matter what, RedWaves-Lite is providing GREAT value.
Dave
User avatar
Asmi Khalil

Site Admin Rank
Posts: 781
Joined: Fri Jun 26, 2015 7:13 am
Display Name: Asmi Khalil
Location: Tunisia

Re: Discrete menus for public and logged-in users/members?

linkPost by admin » Thu Oct 04, 2018 2:19 am

So they are using a different users table and sync shared info between wp_users and eMember users tables. This is a wise decision since they share the same database, you shouldn't have a noticeable delay when a user signs up.

If the above code is not working for you, start by checking members with the highest level then proceed with adding additional checks with lower membership levels. For example, let's say you have the following roles (from high to low): admin, moderator, vip_member, and member.

Use this code:
<?php if ( function_exists( 'wp_emember_is_member_logged_in' ) && wp_emember_is_member_logged_in() ) {

	if ( wp_emember_is_member_logged_in( 'admin' ) ) {
		wp_nav_menu( array( 'menu' => '10', 'menu_class' => 'menu' ) );
	} else if ( wp_emember_is_member_logged_in( 'moderator' ) ) {
		wp_nav_menu( array( 'menu' => '11', 'menu_class' => 'menu' ) );
	} else if ( wp_emember_is_member_logged_in( 'vip_member' ) ) {
		wp_nav_menu( array( 'menu' => '12', 'menu_class' => 'menu' ) );
	} else if ( wp_emember_is_member_logged_in( 'member' ) ) {
		wp_nav_menu( array( 'menu' => '13', 'menu_class' => 'menu' ) );
	}
} else {
	wp_nav_menu( array( 'menu' => '35', 'menu_class' => 'menu' ) );
} ?>
So the only practical workaround is to NOT endow members with anything but the highest level you want them to operate at and be sure the content that you're providing is allowed for all logged in levels.

Maybe they offer a filter to alter this behavior but I am not sure. I never used this plugin before.
It wouldn't surprise me if there is some "tip" in the Tips & Tricks HQ forum that helps work thru this issue, but their forum search is really very difficult to find anything and unless you pay an annual fee, you can't post to their support forum, nor will they answer specific questions. At least they don't require an annual fee to get UPDATES, which was one of the biggest "features" for me several years ago when I purchased the license.
Well, that's sucks. I never wanted to charge a membership fee for regular support but maybe because I don't have thousands of clients. If they have a large client base they need to hire people to do technical support and maybe that's why they charge for support.
But no matter what, RedWaves-Lite is providing GREAT value.
I am Glad you like RedWaves lite :D
I think there is a world market for maybe five computers. - IBM chairman Thomas Watson, 1943

Who is online

Users browsing this forum: No registered users and 1 guest

It is currently Sat Aug 24, 2019 11:30 am