WordPress.org

Make WordPress Core

Opened 3 years ago

Last modified 3 years ago

#39223 closed enhancement

Add a filter to edit $meta array in wpmu_signup_user — at Initial Version

Reported by: Mista-Flo Owned by:
Milestone: 4.8 Priority: normal
Severity: normal Version: 3.0
Component: Users Keywords: has-patch
Focuses: multisite Cc:

Description

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
$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
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.

Change History (1)

@Mista-Flo
3 years ago

Add signup_user_meta filter to edit meta array in wp_signup_user function

Note: See TracTickets for help on using tickets.