Problem

When using Thrive Apprentice and Ontraport’s Pilotpress plugin the Membership levels set on the Ontraport Contact do not correlate to the way that Thrive Apprentice restricts the content. This means that after the contact logs into the Thrive Apprentice wordpress site the Pilotpress functions for protecting content will not work. Thrive Apprentice uses the Wordpress User Roles to restrict the content.

Solution

Since content is restricted on the Role level we need to be able to sync Membership Levels in Pilotpress to WP User Roles.

Standard Wordpress functionality is for each User to only have 1 Role. If your site has the ability to restrict content based on multiple roles, then you can use the WP User Role Editor plugin to add additional roles and this also allows Users to have multiple Roles. That way the Pilotpress Membership Levels and User Roles can properly sync up.

For this solution to work each membership level in Pilotpress must match the User Role in Wordpress. For example if a User Role in Wordpress is ‘Subscriber’ then a Pilotpress membership level should also be named ‘Subscriber’.

The code below can be added to your Wordpress functions.php file which will sync the correlating ‘User Role’ for each Pilotpress Membership Level at the time a member logs in.

If you are not using a Child Theme the functions.php file can be over written on your theme upgrade so if that is the case it is best to add this code as a separate plugin. Download the code as a plugin here

function wct_custom_setup(){
    if( is_user_logged_in() ){
        global $pilotpress;
        if ($pilotpress->get_setting("contact_id", "user")) {
                $user = new \WP_User(get_current_user_id());
                $user_levels = $pilotpress->get_setting("levels", "user", true);
                foreach ($user_levels as $level) {
                    if (in_array($level, $GLOBALS['wp_roles']->role_names) && ($role = array_search($level, $GLOBALS['wp_roles']->role_names)) && !in_array($role, $user->roles)) {
                        $user->add_role($role);
                    }
                }
                $levels = $pilotpress->get_setting("membership_levels", "oap", true);
                foreach ($levels as $level) {
                    if (in_array($level, $GLOBALS['wp_roles']->role_names) && ($role = array_search($level, $GLOBALS['wp_roles']->role_names)) && !in_array($level, $user_levels)) {
                        $user->remove_role($role);
                    }
                }
        }
    }
}
add_action('init', 'wct_custom_setup');