Skip to content

Instantly share code, notes, and snippets.

@crabilld
Created August 8, 2015 10:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save crabilld/f72e80435f27d76269f5 to your computer and use it in GitHub Desktop.
Save crabilld/f72e80435f27d76269f5 to your computer and use it in GitHub Desktop.
Activate PMPro Users with Buddypress
<?php
/*
* This code allows you to use Buddypress' new user activation system in
* conjunction with the registration system in Paid Memberships Pro. When a
* user signs up, they will need to click on the link in their activation
* email before they can login to your site.
*
* This code needs to be added to functions.php in your theme.
*
* Make sure that the Buddypress plugin is activated and the Activate page is
* assigned in Buddypress settings.
*
* Make sure that the Paid Memberships Pro and Paid Memberships Pro - Register
* Helper Add On plugins are activated. The Register Helper plugin actually
* isn't necessary to get user activation to work... it is only used to assign
* the Buddypress profile name field. User activation will still work if you
* remove the add_pmpro_registration_fields and pmpro_set_bp_profile_data
* functions.
*/
// Remove email confirmation field from checkout
add_filter( 'pmpro_checkout_confirm_email', '__return_false' );
// Add name field to PMPro checkout page
function add_pmpro_registration_fields() {
if ( ! function_exists( 'pmprorh_add_registration_field' ) ) {
return false;
}
if ( is_user_logged_in() ) { // Don't show field if user is logged in
return false;
}
pmprorh_add_registration_field(
'after_username',
new PMProRH_Field(
'pmpro_name',
'text',
array(
'label' => 'Name',
'required' => true,
'save_function' => 'pmpro_set_bp_profile_data',
)
)
);
}
add_action( 'init', 'add_pmpro_registration_fields' );
// Set Buddypress profile name from PMPro name field
function pmpro_set_bp_profile_data( $user_id, $field, $value ) {
if ( ! function_exists( 'xprofile_set_field_data' ) ) {
return false;
}
if ( 'pmpro_name' == $field ) {
// Set name field
xprofile_set_field_data( bp_xprofile_fullname_field_id(), $user_id, $value );
// Update WP name fields
xprofile_sync_wp_profile( $user_id );
}
}
// Add new user via Buddypress after checkout
function add_new_user_with_buddypress( $user_id, $userdata ) {
if ( ! function_exists( 'bp_core_signup_user' ) ) {
return $user_id;
}
do_action( 'bp_signup_validate' );
$usermeta = array( 'field_1' => $_REQUEST['pmpro_name'] );
$user_id = bp_core_signup_user( $userdata['user_login'], $userdata['user_pass'], $userdata['user_email'], $usermeta );
do_action( 'bp_complete_signup' );
return $user_id;
}
add_filter( 'pmpro_new_user', 'add_new_user_with_buddypress', 10, 2 );
// Do not run PMPro user setup, which would automatically login the user
add_filter( 'pmpro_setup_new_user', '__return_false' );
// Change confirmation url for users needing to activate their account
function change_pmpro_confirmation_url( $url, $user_id, $pmpro_level ) {
if ( ! function_exists( 'bp_get_activate_slug' ) ) {
return $url;
}
// Redirect new / inactive users to the activation page
if ( ! ( empty( $user_id ) || is_wp_error( $user_id ) ) && 2 == BP_Signup::check_user_status( $user_id ) ) {
return '/' . add_query_arg( 'redirect', 'checkout', bp_get_activate_slug() );
}
return $url;
}
add_filter( 'pmpro_confirmation_url', 'change_pmpro_confirmation_url', 10, 3 );
// Add confirmation message to activation page
function add_bp_activation_info() {
if ( 'checkout' == $_GET['redirect'] && ! bp_account_was_activated() ) { ?>
<div id="message" class="bp-template-notice updated">
<p><?php _e( 'You have successfully created your account! To begin using this site you will need to activate your account via the email we have just sent to your address.', 'buddypress' ); ?></p>
</div>
<?php }
}
add_action( 'bp_before_activate_content', 'add_bp_activation_info' );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment