If you are a developer and want to customize the code or if you want to add conditions around your content then the code examples from this page will be helpful.
Note: If you don’t have PHP coding skills then you should hire a developer to do customization related tweaks.
You can add code customization to your site by adding it to the functions.php file of your theme. Alternatively, you can create a custom little plugin and add the code customization to that plugin.
Check 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)
SwpmMemberUtils::is_member_logged_in()
You can utilize the above function in the following way to add conditional block (for example: show a special message to your logged in users):
<?php if(SwpmMemberUtils::is_member_logged_in()) { ?> //Your message for logged-in members goes here <?php } ?>
The following example can be useful if you want to show ad to non-members of the site:
<?php if(!SwpmMemberUtils::is_member_logged_in()) { ?> //Your ad code goes here <?php } ?>
Retrieve Member ID of the Logged in Member
The following code will retrieve the currently logged-in user’s ID:
$member_id = SwpmMemberUtils::get_logged_in_members_id();
Show Specific Content to a Particular Member
The following code will show a message to the member if his/her ID is “2”:
$member_id = SwpmMemberUtils::get_logged_in_members_id(); if ($member_id == "2"){ echo "Hi, your member ID is 2"; }
Retrieve the Member’s Membership Level
The following code will retrieve the currently logged-in user’s membership level:
$member_level = SwpmMemberUtils::get_logged_in_members_level();
Show Custom Content Based on Membership Level
The following example code demonstrates how you can show some content to a member of a particular membership level:
//First check if the visitor viewing the page is logged into the site. if(!SwpmMemberUtils::is_member_logged_in()) { //User is not logged-in. So don't go forward. return 0; } //Lets see what level this member belongs to $member_level = SwpmMemberUtils::get_logged_in_members_level(); if ($member_level == '2'){ //This member has level 2. //Show something that will be visible to member with membership level 2. } else if ($member_level == '3') { //This member has level 3 //Show something that will be visible to member with membership level 3. }
Creating Encrypted Password
$encrypted_pass = SwpmUtils::encrypt_password($plain_password);
Retrieve a Member’s Record
The following code will retrieve the member’s record given the member ID of the user:
$member_id = '1'; $swpm_user = SwpmMemberUtils::get_user_by_id($member_id); //Uncomment the following line to see the retrieved member info. //var_dump($swpm_user);
The following code will retrieve the member’s record given the member’s username:
$username = 'test123'; $swpm_user = SwpmMemberUtils::get_user_by_user_name($username);
The following code will retrieve the member’s record given the member’s email address:
$member_email = 'test123@yahoo.com'; $swpm_user = SwpmMemberUtils::get_user_by_email($member_email);
Retrieve a Member’s Profile Field Info
The following code will retrieve the specified member’s email address:
$member_id = '1'; $field_name = 'email'; $email_value = SwpmMemberUtils::get_member_field_by_id($member_id, $field_name);
The following code will retrieve the specified member’s first and last names:
$member_id = '1'; $field_name = 'first_name'; $fname_value = SwpmMemberUtils::get_member_field_by_id($member_id, $field_name);
$field_name = 'last_name'; $lname_value = SwpmMemberUtils::get_member_field_by_id($member_id, $field_name);
The following code will retrieve the specified member’s membership level:
$member_id = '1'; $field_name = 'membership_level'; $level_id_value = SwpmMemberUtils::get_member_field_by_id($member_id, $field_name);
Check If Logged-in Member Has Access to a Post
The following code snippet gives you an example of how you can check if the logged-in member has access to a particular post (given the post ID)
global $post; $access_ctrl = SwpmAccessControl::get_instance(); if ($access_ctrl->can_i_read_post($post)){ //This user can read the specified post //Add your custom code that does something }
Getting the Expiry Date of the Logged-in Member
The following code snippet will retrieve the expiry date of the member who is logged into the site and viewing the page:
<?php if(SwpmMemberUtils::is_member_logged_in()) { $auth = SwpmAuth::get_instance(); $expiry_date = $auth->get_expire_date(); echo "Expiry date: ".$expiry_date; } else{ echo "You are not logged-in as a member"; } ?>
Create an After Logout Redirection
The following example code will redirect the members to a specified page when they logout.
add_action('swpm_logout', 'my_custom_after_logout_redirection'); function my_custom_after_logout_redirection() { //Specify the URL that you want to redirect to $url = "http://www.example.com/after-logout-page"; wp_redirect($url); exit(); }
Check Current User’s Post Access Permission then Redirect User
The following example code checks if the current user has access to the post being viewed then redirects the user if he/she doesn’t have access to it.
global $post; $post_id = $post->ID;//Grab the post ID $protection_ctrl = SwpmProtection::get_instance(); //Check if the current visitor has access to this post. $is_protected = $protection_ctrl->is_protected($post_id); if($is_protected) { //This user doesn't have permission to see this post. Redirect the user. $redirect_url = 'http://example.com/redirect-page'; wp_redirect($redirect_url); exit; }
Update a Member’s Account Status from Your Custom PHP Code
The following example code will update the member’s (whose member ID is 2) account status to “active”:
$member_id = "2"; SwpmMemberUtils::update_account_state($member_id, 'active');
Create an After Registration Redirection
There is now a feature for it in the core plugin.
The following example code will redirect the users to a specified page after they submit the registration form.
add_action('swpm_front_end_registration_complete', 'do_after_rego_redirection'); add_action('swpm_front_end_registration_complete_fb', 'do_after_rego_redirection');//For from builder addon function do_after_rego_redirection() { //TODO - Specify the URL that you want to redirect to $url = 'http://www.yourwebsite.com/thank-you-for-registering/'; wp_redirect($url); }
Disable the Registration Complete Email
Use the following PHP tweak to disable the registration complete email. Useful if you don’t want to send the registration complete email after a member registers.
add_filter('swpm_email_registration_complete_body', 'disable_rego_complete_email'); function disable_rego_complete_email($email_body) { //Empty the email body (this will disable the email) $email_body = ''; return $email_body; }