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 */
user_register
runs. wordpress.stackexchange.com/questions/155566/…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 triedwp_insert_user()
and your code worked fine.um_user()
retrieves a user meta/data. So perhaps the answer can help? :)