Advertisement
vapvarun

Disable activation email and auto-activate users in BuddyPress

Sep 8th, 2024
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.08 KB | Software | 0 0
  1. <?php
  2. // Disable activation email and auto-activate users in BuddyPress
  3.  
  4. function wbcom_disable_activation_email() {
  5.     remove_action( 'bp_core_signup_send_validation_email', 'bp_core_signup_send_validation_email' );
  6.     error_log( "Activation email disabled." );
  7. }
  8. add_action( 'bp_init', 'wbcom_disable_activation_email' );
  9.  
  10. // Automatically activate user signups
  11. function wbcom_auto_activate_user( $user_login ) {
  12.     global $wpdb;
  13.  
  14.     // Get the signup entry using BP_Signup class
  15.     $signup_data = BP_Signup::get(
  16.         array(
  17.             'user_login' => $user_login,
  18.             'active'     => 0, // Only look for inactive users
  19.         )
  20.     );
  21.  
  22.     // Check if the signup exists
  23.     if ( ! empty( $signup_data['signups'] ) ) {
  24.         $signup = $signup_data['signups'][0]; // Assuming the first result is correct
  25.  
  26.         // Activate the signup using BuddyPress' activation method
  27.         $activation_result = BP_Signup::activate( array( $signup->signup_id ) );
  28.  
  29.         if ( isset( $activation_result['activated'] ) && ! empty( $activation_result['activated'] ) ) {
  30.             $activated_user = $activation_result['activated'][0];
  31.  
  32.             // Log the successful activation
  33.             error_log( "User successfully activated: " . $activated_user->ID );
  34.  
  35.             // Automatically log in the user after activation
  36.             wp_set_auth_cookie( $activated_user->ID, false, is_ssl() );
  37.             wp_set_current_user( $activated_user->ID );
  38.             error_log( "User logged in: " . $activated_user->ID );
  39.  
  40.             // Redirect the user to their profile page
  41.             bp_core_redirect( bp_core_get_user_domain( $activated_user->ID ) );
  42.         } else {
  43.             error_log( "Error activating user: " . $signup->user_login );
  44.         }
  45.     } else {
  46.         error_log( "User signup not found or already activated: " . $user_login );
  47.     }
  48. }
  49.  
  50. // Hook into the registration process
  51. add_action( 'bp_core_signup_user', function( $user_id, $user_login, $user_password, $user_email, $usermeta ) {
  52.     wbcom_auto_activate_user( $user_login );
  53. }, 10, 5 );
  54.  
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement