Defined in classes/wp_user_sync.php
PHP class used to manage relationships between PrivateContent users and their WordPress mirrors: sync, detach, checks.
It is instantiated on plugin's load and reachable through the $pc_wp_user PHP global. It extends the main pc_users class.
Synced WordPress users have the pvtcontent role (plus the custom roles set in settings) and share the same password hash of the PrivateContent user.
Syncs every PrivateContent user not synced yet. Users must have a unique e-mail and username to be synced.
Usage Parameters No parameters
Return values Type Description (string)message reporting how many users have been synced and how many failed
Creates the WordPress mirror of a PrivateContent user, or updates an existing one. On creation, the pc_user_synced_with_wp action is triggered.
Usage Copy sync_wp_user($user_data = array(), $existing_id = 0, $save_in_db = false) Parameters Parameter Description (array) $user_datadata to use for the WP user - check the table below (int) $existing_idsynced WordPress user ID to update. Zero to create a new one (bool) $save_in_dbwhether to save the created WordPress user ID in the PrivateContent user record
User data Key Notes (string) usernamerequired creating a new user (string) emailrequired creating a new user (string) pswrequired creating a new user. It must be the hashed password stored in the PrivateContent database (int) idPrivateContent user ID, stored in the pc_user_id WP user meta (string) nameused as WP first name (string) surnameused as WP last name
Return values Type Description (int)created/updated WordPress user ID (false)if an error occurred (check the $pwu_sync_error property)
Example Copy global $pc_users, $pc_wp_user;
$data = $pc_users->get_user(5, array('to_get' => array('id', 'username', 'email', 'psw', 'name', 'surname')));
$wp_user_id = $pc_wp_user->sync_wp_user($data, 0, true);
if(!$wp_user_id) {
$error = $pc_wp_user->pwu_sync_error;
} Copies a PrivateContent password hash into the synced WordPress user.
Usage Copy sync_psw_to_wp($psw, $wp_user_id) Parameters Parameter Description (string) $pswalready hashed password(int) $wp_user_idWordPress user ID
Return values Type Description (int|false)$wpdb->update() response
Copies a WordPress user password hash into the synced PrivateContent user. If that user is logged, its login cookie is updated.
Usage Copy sync_psw_to_pc($wp_user_id, $pc_user_id) Parameters Parameter Description (int) $wp_user_idWordPress user ID (int) $pc_user_idPrivateContent user ID
Return values Type Description (bool)true if the password has been synced
search_and_sync_matches() method # Searches matches between unsynced PrivateContent users and WordPress users having the same username and e-mail, then syncs them. Be careful : matched WordPress users become PrivateContent mirrors (role and password are overwritten).
Usage Copy search_and_sync_matches() Parameters No parameters
Return values Type Description (string)message reporting how many users have been synced
Creates a PrivateContent user starting from an existing WordPress user, then syncs them (the WordPress password is kept). Administrators and PrivateContent admins can't be used. On success the pc_user_created_from_wp action is triggered.
Usage Copy pc_user_from_wp($wp_user_id, $pc_cats) Parameters Parameter Description (int) $wp_user_idWordPress user ID (array) $pc_catsuser category IDs to assign
Return values Type Description (int)created PrivateContent user ID (WP_Error)if the user can't be created
Example Copy global $pc_wp_user;
$result = $pc_wp_user->pc_user_from_wp(12, array(3));
if(is_wp_error($result)) {
echo esc_html($result->get_error_message());
} Detaches every synced user. WordPress mirror users are deleted .
Usage Parameters No parameters
Return values Type Description (string)message confirming the operation
Detaches a PrivateContent user from its WordPress mirror, deleting the WordPress user. The pc_user_detached_from_wp action is triggered right before the deletion.
Usage Copy detach_wp_user($user_id, $save_in_db = true, $wp_user_id = false) Parameters Parameter Description (int) $user_idPrivateContent user ID (bool) $save_in_dbwhether to update the PrivateContent user record (int|false) $wp_user_idsynced WordPress user ID, if already known (saves a query)
Return values Type Description (bool)true, or false if WordPress user deletion functions are not available
Example Copy global $pc_wp_user;
$pc_wp_user->detach_wp_user(5); wp_user_is_linked() method # Checks whether a WordPress user is synced with a PrivateContent one.
Usage Copy wp_user_is_linked($wp_user_id) Parameters Parameter Description (int) $wp_user_idWordPress user ID
Return values Type Description (object)PrivateContent user data object, containing id, username, categories (serialized), psw (hash) and status (null|false)if the user is not synced
Example Copy global $pc_wp_user;
$pc_user = $pc_wp_user->wp_user_is_linked(get_current_user_id());
if($pc_user) {
$pc_user_id = (int)$pc_user->id;
} Checks whether a PrivateContent user is synced with a WordPress one.
Usage Copy pvtc_is_synced($user_id, $return_id = false) Parameters Parameter Description (int) $user_idPrivateContent user ID (bool) $return_idwhether to directly return the WordPress user ID
Return values Type Description (WP_User|int)WordPress user object (got through get_userdata()) or its ID (false)if user is not synced
manual_user_login() method # Logs in a synced WordPress user (used by PrivateContent login systems). Any other logged WordPress user is logged out. Triggers the WordPress wp_login action.
Usage Copy manual_user_login($wp_user_id, $username, $remember_me = false) Parameters Parameter Description (int) $wp_user_idWordPress user ID (string) $usernameWordPress username (bool) $remember_mewhether to use a long-lasting authentication cookie
Use pc_login() to log in users: it also manages the WordPress mirror login.
Updates a WordPress user nicename and display name, using its first and last name (or username).
Usage Copy update_nicename($wp_user_id) Parameters Parameter Description (int) $wp_user_idWordPress user ID
Return values No value returned
Checks whether an e-mail can be assigned to a WordPress user (not used by other WordPress users).
Usage Copy new_mail_is_ok($wp_user_id, $email) Parameters Parameter Description (int) $wp_user_idWordPress user ID (string) $emailnew e-mail
Return values Type Description (bool)true if the e-mail can be used
Checks whether a username or an e-mail are already used by a WordPress user.
Usage Copy wp_user_exists($username, $email) Parameters Parameter Description (string) $usernameusername to check (string) $emaile-mail to check
Return values Type Description (bool)true if one of them is used, otherwise false
get_wps_custom_roles() method # Gets roles assigned to synced users: pvtcontent plus the custom ones set in settings.
Usage Copy get_wps_custom_roles() Parameters No parameters
Return values Type Description (array)role slugs
set_wps_custom_roles() method # Applies roles set in settings to synced WordPress users.
Usage Copy set_wps_custom_roles($wp_user_id = false, $is_new_user = false) Parameters Parameter Description (int|array|false) $wp_user_idWordPress user ID or IDs array. False to update every synced user (bool) $is_new_userif true, skips the update when the only role to assign is pvtcontent
Return values Type Description (bool)true if the operation succeeded, otherwise false
role_is_privileged() method # Checks whether a WordPress role has administrative capabilities (listed in the $privileged_caps property).
Usage Copy role_is_privileged($role_id) Parameters Parameter Description (string) $role_idrole slug
Return values Type Description (bool)true if the role is privileged
wp_user_is_privileged() method # Checks whether a WordPress user has administrative capabilities (listed in the $privileged_caps property).
Usage Copy wp_user_is_privileged($wp_user_id) Parameters Parameter Description (int) $wp_user_idWordPress user ID
Return values Type Description (bool)true if the user is privileged