VOOZH about

URL: https://simple-membership-plugin.com/simple-membership-miscellaneous-php-tweaks/

⇱ Simple Membership Miscellaneous Tweaks


Simple Membership Miscellaneous PHP Tweaks

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 may need to 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.

Table of Contents

Login Status

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 } ?>

Working with Member Records

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();

Retrieve Member ID by Username

If you have a member’s username, you can retrieve their Member ID using the following code:

$username = "john234";
$profile_data = SwpmMemberUtils::get_user_by_user_name($username);
$member_id = $profile_data->member_id;

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 the Member’s Membership Level

The following code retrieves the currently logged-in user’s membership level:

$member_level = SwpmMemberUtils::get_logged_in_members_level();

The following code retrieves the membership level ID for the specified member:

$level_id = SwpmMemberUtils::get_membership_level_id_of_a_member($member_id);

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);

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";
}
?>

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');

Creating Encrypted Password

$encrypted_pass = SwpmUtils::encrypt_password($plain_password);

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";
}

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
}

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.
}

Programmatically Apply Content Protection to a Post or Page

You can use the SwpmProtection::apply_protection_to_post() function to programmatically apply content protection to a specific post or page. Below is an example demonstrating how to protect a post and restrict access to specific membership levels:

// The ID of the post you want to protect
$post_id = 50;
// Membership level IDs that should have access
$allowed_levels = array(2, 4);
// Apply protection to the post/page.
SwpmProtection::apply_protection_to_post($post_id, $allowed_levels);

Programmatically Apply Content Protection to Multiple Posts or Pages

You can use the SwpmProtection::apply_protection_to_posts() function to programmatically apply content protection to multiple posts or pages at once. This is useful when you want to restrict access to several items based on membership levels. See the example below:

// IDs of the posts or pages you want to protect
$post_ids = array(50, 52, 55);
// Membership level IDs that should have access
$allowed_levels = array(2, 4);
// Apply protection to the specified posts/pages
SwpmProtection::apply_protection_to_posts($post_ids, $allowed_levels);

Check Current User’s Access to a Post and Redirect if Necessary

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;
}

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();
}

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);
}

Miscellaneous

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;
}

Override or Customize Template Files

The Simple Membership plugin loads certain user-visible template files using the swpm_load_template() function, allowing developers to load a customized version of the template. If you want to customize the login and other template files, follow this tutorial to learn how to do it.