1

I want to update post meta when the user registers based on the Ultimate Member plugin field in the register form. Here is the code which is working fine if I run directly on the template (loading page) but not working with the hook.

Note: As I mention the code is working as expected if I run on template file (refreshing page) but not with the hook.

function gs_add_user_to_group($user_id)
{
    // get the user group by profile
    $groupItem = get_post(um_user('team'));

    // check if post is exists and is a group post type
    if ($groupItem && $groupItem->post_type == Group_Shop_Admin::get_cpt_group()) {

        // get group post meta
        $meta         = gs_get_group_meta_key('users');
        $old_user_ids = get_post_meta($groupItem->ID, $meta, TRUE);

        // check if empty
        if ( ! $old_user_ids) {
            $user_ids = [];
        } else {
            $user_ids = $old_user_ids;
        }

        // push newly register user id
        array_push($user_ids, $user_id);

        if ( ! $old_user_ids) {
            // add new record if not exits
            add_post_meta($groupItem->ID, $meta, $user_ids);
        } else {
            // update and add newly register user id to record
            update_post_meta($groupItem->ID, $meta, $user_ids, $old_user_ids);
        }
    }
}

add_action('user_register', 'gs_add_user_to_group', 10, 1);
/* End of  gs_add_user_to_group hook */
5
  • 1
    Usermeta doesn't exist in the database yet when user_register runs. wordpress.stackexchange.com/questions/155566/…
    – WebElaine
    Mar 9 '20 at 16:22
  • @WebElaine that is what I thought. So is there any alternatives? Mar 10 '20 at 1:14
  • By "run directly", did you mean you manually called gs_add_user_to_group() in your template? And how did you (or what steps you took which let you) know it doesn't work with the hook - and by that, you mean your function isn't getting called or that the post meta is not being added or updated? And I actually tried wp_insert_user() and your code worked fine.
    – Sally CJ
    Mar 10 '20 at 6:15
  • I have directly checked into the database. So when running on template it is updating the record but through the action hook. Mar 10 '20 at 6:40
  • 1
    I see it now. um_user() retrieves a user meta/data. So perhaps the answer can help? :)
    – Sally CJ
    Mar 10 '20 at 10:07
1

The part that does not work in your code is not adding or updating the post meta, but getting the user meta named team which you're retrieving via um_user().

And that's because as @WebElaine pointed, by the time the user_register hook is fired, the custom field team has not yet added to the database (neither by WordPress nor UM), hence um_user( 'team' ) returns nothing/false.

But if you want to use the user_register hook to add/update the post meta, you can use the $_POST['team'] value submitted via the form. So replace this:

$groupItem = get_post(um_user('team'));

with this:

if ( $post_id = um_user( 'team' ) ) {
    $groupItem = get_post( $post_id );
// I'm just doing a basic validation, but you should try to make it better!
} elseif ( ! empty( $_POST['team'] ) ) {
    $groupItem = get_post( absint( $_POST['team'] ) );
}

Or since you're relying upon the team meta which came from user input submitted via the UM's registration form, then it might better to use um_registration_complete hook to add/update the post meta:

  1. First, call um_fetch_user() before you call um_user():

    um_fetch_user( $user_id ); // set the user
    $groupItem = get_post( um_user( 'team' ) );
    

    Alternatively, you can use get_user_meta():

    $post_id = get_user_meta( $user_id, 'team', true );
    $groupItem = get_post( $post_id );
    
  2. And replace the add_action('user_register', 'gs_add_user_to_group', 10, 1); with this:

    add_action( 'um_registration_complete', 'gs_add_user_to_group', 10, 2 );
    

Tried & tested working on WordPress 5.3.2 with UM 2.1.4.

3
  • 1
    Amazing help. Thanks a lot. That's the hook I was looking some hours ago and going to give it a try. Before that, I read your answer and found even if I tried, I would miss um_fetch_user( $user_id ); :) thanks once again. (y) Mar 10 '20 at 12:31
  • 1
    You're welcome, and I also actually almost missed that um_fetch_user() thing. :) Also, you don't need to use the 10, 2 in the add_action() call.. I use the 2 when checking the $args passed via that hook. :)
    – Sally CJ
    Mar 10 '20 at 16:54
  • 1
    Cool, thanks to a lot. That will help too. Mar 11 '20 at 3:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.