wpmu_activate_signup

Activate a signup.

Description

(array|WP_Error) wpmu_activate_signup( (string) $key ); 

Hook to or for events that should happen only when users or sites are self-created (since those actions are not called when users and sites are created by a Super Admin).

Returns (array|WP_Error)

An array containing information about the activated user and/or blog

Parameters (1)

0. $key (string)
The activation key provided to the user.

Usage

  1. if ( !function_exists( 'wpmu_activate_signup' ) ) { 
  2. require_once ABSPATH . WPINC . '/ms-functions.php'; 
  3.  
  4. // The activation key provided to the user. 
  5. $key = ''; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = wpmu_activate_signup($key); 
  9.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/ms-functions.php  
  1. function wpmu_activate_signup($key) { 
  2. global $wpdb; 
  3.  
  4. $signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE activation_key = %s", $key) ); 
  5.  
  6. if ( empty( $signup ) ) 
  7. return new WP_Error( 'invalid_key', __( 'Invalid activation key.' ) ); 
  8.  
  9. if ( $signup->active ) { 
  10. if ( empty( $signup->domain ) ) 
  11. return new WP_Error( 'already_active', __( 'The user is already active.' ), $signup ); 
  12. else 
  13. return new WP_Error( 'already_active', __( 'The site is already active.' ), $signup ); 
  14.  
  15. $meta = maybe_unserialize($signup->meta); 
  16. $password = wp_generate_password( 12, false ); 
  17.  
  18. $user_id = username_exists($signup->user_login); 
  19.  
  20. if ( ! $user_id ) 
  21. $user_id = wpmu_create_user($signup->user_login, $password, $signup->user_email); 
  22. else 
  23. $user_already_exists = true; 
  24.  
  25. if ( ! $user_id ) 
  26. return new WP_Error('create_user', __('Could not create user'), $signup); 
  27.  
  28. $now = current_time('mysql', true); 
  29.  
  30. if ( empty($signup->domain) ) { 
  31. $wpdb->update( $wpdb->signups, array('active' => 1, 'activated' => $now), array('activation_key' => $key) ); 
  32.  
  33. if ( isset( $user_already_exists ) ) 
  34. return new WP_Error( 'user_already_exists', __( 'That username is already activated.' ), $signup); 
  35.  
  36. /** 
  37. * Fires immediately after a new user is activated. 
  38. * @since MU 
  39. * @param int $user_id User ID. 
  40. * @param int $password User password. 
  41. * @param array $meta Signup meta data. 
  42. */ 
  43. do_action( 'wpmu_activate_user', $user_id, $password, $meta ); 
  44. return array( 'user_id' => $user_id, 'password' => $password, 'meta' => $meta ); 
  45.  
  46. $blog_id = wpmu_create_blog( $signup->domain, $signup->path, $signup->title, $user_id, $meta, $wpdb->siteid ); 
  47.  
  48. // TODO: What to do if we create a user but cannot create a blog? 
  49. if ( is_wp_error($blog_id) ) { 
  50. // If blog is taken, that means a previous attempt to activate this blog failed in between creating the blog and 
  51. // setting the activation flag. Let's just set the active flag and instruct the user to reset their password. 
  52. if ( 'blog_taken' == $blog_id->get_error_code() ) { 
  53. $blog_id->add_data( $signup ); 
  54. $wpdb->update( $wpdb->signups, array( 'active' => 1, 'activated' => $now ), array( 'activation_key' => $key ) ); 
  55. return $blog_id; 
  56.  
  57. $wpdb->update( $wpdb->signups, array('active' => 1, 'activated' => $now), array('activation_key' => $key) ); 
  58. /** 
  59. * Fires immediately after a site is activated. 
  60. * @since MU 
  61. * @param int $blog_id Blog ID. 
  62. * @param int $user_id User ID. 
  63. * @param int $password User password. 
  64. * @param string $signup_title Site title. 
  65. * @param array $meta Signup meta data. 
  66. */ 
  67. do_action( 'wpmu_activate_blog', $blog_id, $user_id, $password, $signup->title, $meta ); 
  68.  
  69. return array('blog_id' => $blog_id, 'user_id' => $user_id, 'password' => $password, 'title' => $signup->title, 'meta' => $meta);