If you are a developer and want to customize the code or if you want to add conditions around your content then the following code examples should be helpful.
Find out if a member is logged in or not
You can use the following function call to determine if a member is logged in or not. The following function call will return true if a member is logged in (otherwise it will return false)
wp_emember_is_member_logged_in()
You can utilize the above function in the following way to ad code in your theme’s template file to add conditional block (for example: show a special message to your logged in users):
<?php if(wp_emember_is_member_logged_in()) { ?> //Place your special message for members here <?php } ?>
The following example can be useful if you want to show ad to non-members of the site:
<?php if(!wp_emember_is_member_logged_in()) { ?> //Place your ad code here <?php } ?>
The following example can be useful if you want to show a message to a member who belongs to membership level 1:
<?php if(wp_emember_is_member_logged_in('1')) { ?> //Place your special message for members from membership level with ID 1 <?php } ?>
The following PHP code example shows how you can programmatically retrieve the member ID of a member and add condition:
<?php $emember_auth = Emember_Auth::getInstance(); $user_id = $emember_auth->getUserInfo('member_id'); if (!empty($user_id)) { //pleace code that only members can see } ?>
Hiding Post Title From Non Members
We will use the “wp_emember_is_member_logged_in” function (explained above) to modify the “Twenty Eleven” theme and make the post title only visible to users who are logged in.
This is an example for when a post is displayed on a single page – ie, I edited the content-single.php file:
The original line of code was as follows:
<h1 class="entry-title"><?php the_title(); ?>
To hide the post heading/title , I modified it as follows:
<h1 class="entry-title"><?php if(wp_emember_is_member_logged_in()) the_title(); ?>
The above is a simple example which will suppress the post heading if someone is not logged in.
Get member id and his/her membership level
<?php $emember_auth = Emember_Auth::getInstance(); $user_id = $emember_auth->getUserInfo('member_id'); if (!empty($user_id)) { //User is logged in so add your conditional code here $membership_level = $emember_auth->getUserInfo('membership_level'); if($membership_level == 1) { //Add stuff for this level } } ?>
Get the homepage URL from the logged in member’s membership level
<?php $emember_auth = Emember_Auth::getInstance(); $user_id = $emember_auth->getUserInfo('member_id'); if (!empty($user_id)) { //User is logged in so add your conditional code here $membership_level_resultset = $emember_auth->userInfo->primary_membership_level; $home_page_url = $membership_level_resultset->loginredirect_page; //Do something with this URL } ?>
Show Logged in Member’s Username or other details
<?php $emember_auth = Emember_Auth::getInstance(); $username = $emember_auth->getUserInfo('user_name'); echo "Username of the logged in member: ".$username; ?>
Check if the Logged in Member is Allowed to See a Post
<?php $emember_auth = Emember_Auth::getInstance(); $post_id = "10" if($emember_auth->is_protected_post($post_id)) { //This member is allowed to see this post }
Retrieve Member’s Data
- How to retrieve logged-in member’s data using a shortcode or PHP code
- How to retrieve a particular member’s data using shortcode or PHP
Retrieve a Member Record Using the Username
The following PHP code will retrieve a member’s record for the given username:
<?php $username = "johndoe";//Replace with the actual username $member = emember_get_member_by_username ($username); echo "Member's First Name: " . $member->first_name; //Output the first name echo "Member's Last Name: " . $member->last_name; //Output the last name ?>
Retrieve Total Members Count
The following PHP code will display the total members count:
<?php echo emember_get_total_members(array()); ?>
Member Registration Completion Hook
If you need to execute some custom code after a member completes the registration, use the following action hook:
Show Different Navigation Menu to your Members and Non-members
Show Logged in Member’s Details Using Shortcode
You can use a shrotcode to show any details of the logged in member on a WordPress post or page. Refer to the eMember shortcode documentation for more details.
Placing a Registration Form For a Particular Membership Level to Give Backdoor Entrance
Free Members Must Confirm Their Email Address
Get the Login Page’s URL from the System
$emember_config = Emember_Config::getInstance(); $login_page_url = $emember_config->getValue('login_page_url');
Get the Registration Page’s URL from the System
$emember_config = Emember_Config::getInstance(); $login_page_url = $emember_config->getValue('eMember_registration_page');
Get the Edit Profile Page’s URL from the System
$emember_config = Emember_Config::getInstance(); $login_page_url = $emember_config->getValue('eMember_profile_edit_page');
@Viviana, You can use the following function which returns an array of all the membership levels that the given member has. Then check if any of the level ID matches.
$all_level_ids = emember_get_all_level_ids_of_a_member($member_id);
Hi! I have multiple membership levels for my users in my site. How can I retrieve and check whether a member is in a membership level? The code above only refers to the primary membership level. Thanks!
There are a few ways to do this… it all depends on your choice but you basically just need to enter the following shortcode on your “Join Us” page rather than telling them to the go directly to the “registration page”:
[free_rego_with_email_confirmation]
To simplify the above. You should have already created a Register or Registration page that has this shortcode:
[wp_eMember_registration_form:end]
1. Open the “old” Register/Registration page in your WP dashboard. Replace the original shortcode above with:
[free_rego_with_email_confirmation]
2. Cut and paste the original shortcode to the new Free Membership page:
[wp_eMember_registration_form:end]
3. In eMember control panel go to: WP eMembers – Pages Settings => Registration Page:
Change the path name to the new free membership page:
./pathname/free-membership-page-name/
4. Tweak copy on new Free Membership page to suit.