[wp-trac] [WordPress Trac] #39223: Add a filter to edit $meta array in wpmu_signup_user

WordPress Trac noreply at wordpress.org
Sat Dec 10 11:38:41 UTC 2016


#39223: Add a filter to edit $meta array in wpmu_signup_user
-------------------------+-----------------------------
 Reporter:  Mista-Flo    |      Owner:
     Type:  enhancement  |     Status:  new
 Priority:  normal       |  Milestone:  Awaiting Review
Component:  Users        |    Version:  3.0
 Severity:  normal       |   Keywords:
  Focuses:  multisite    |
-------------------------+-----------------------------
 Hello,

 Here's the situation.

 In a multisite context, on a new user form in a site of the network, it
 creates a new signup entry in the signups database table with user
 informations, and a meta array containing by default {{{ 'add_to_blog' =>
 $wpdb->blogid, 'new_role' => $_REQUEST['role'] }}}, so when the user is
 finnaly activated (with email confirmation or not) the
 {{{wpmu_activate_user}}} function is called and use this $meta array.

 Imagine you want to extend this behavior and for example, add some user
 meta in signup to use it for activation. you have the good hook in
 wpmu_activate_user {{{wpmu_activate_user}}} where you can use the meta
 array to do what you want, but you can't filter the $meta array in
 {{{wpmu_signup_user}}} to add some usefull data.

 For example, for my plugin Multiple Roles : https://wordpress.org/plugins
 /multiple-roles/ which allow admins to add multiple roles for a given
 user. In order to handle the multisite behavior, I need to edit this $meta
 array to pass it my multiple roles array.

 So according to me, the best approach will be to add a filter for $meta
 array in {{{wpmu_signup_user}}} function just before the database insert
 like this :

 {{{#!php
 <?php
 $meta = apply_filters( 'signup_user_meta', $meta, $user, $user_email, $key
 );
 }}}

 With this given filter, In my plugin, I just have to add those lines, so
 here a real use of case :
 {{{#!php
 <?php
 add_filter( 'signup_user_meta', 'mu_add_roles_in_signup', 10, 4 ); //
 Handle Multisite
 add_action( 'wpmu_activate_user', 'mu_add_roles_after_activation', 10, 3
 ); // Handle Multisite

         /**
          * Add multiple roles in meta array on multisite signups database
 table
          *
          * @param $meta
          * @param $user
          * @param $user_email
          * @param $key
          *
          * @return mixed
          */
         function mu_add_roles_in_signup( $meta, $user, $user_email, $key )
 {

                 $new_roles = ( isset( $_POST['md_multiple_roles'] ) &&
 is_array( $_POST['md_multiple_roles'] ) ) ? $_POST['md_multiple_roles'] :
 array();
                 if ( ! empty( $new_roles ) ) {
                         $meta['md_roles'] = $new_roles;
                 }

                 return $meta;
         }

         /**
          * Add multiple roles after user activation
          *
          * @param $user_id
          * @param $password
          * @param $meta
          */
         function mu_add_roles_after_activation( $user_id, $password, $meta
 ) {
                 if ( ! empty( $meta['md_roles'] ) ) {
                         $this->model->update_roles( $user_id,
 $meta['md_roles'] );
                 }
         }

 }}}

 By the way, if you're okay with my following patch, I would benefit from
 this other ticket : https://core.trac.wordpress.org/ticket/38781 which
 avoid $meta array to be serialized when passed to hooks, in fact, I'm
 using its patch to make mine.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/39223>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list