• Resolved celinehag

    (@celinehag)


    Hi,
    after migration of UM users to another website ‘form_id’ in wp_usermeta no longer matches.

    It seems I need to change form_id for all users in wp_usermeta table. The form_id value needs to be updated in two places for each user:

    1. in the ‘form_id’ field
    2. inside the serialized array in the ‘submitted’ field

    This can be done programmatically by using update_user_meta() – function.

    Something along these lines:

    $user_id = $some_user_id;
        $toupdate= array(
            'form_id' => $some_form_id,
    	'submitted' => ????
        );
        foreach( $toupdate as $k => $new_val ){ 
          update_user_meta($user_id, $k, $new_val );
        }

    I can’t figure out how to target ‘form_id’ inside the serialized submitted array.
    Any help here is very appreciated.

    Regards

Viewing 2 replies - 1 through 2 (of 2 total)
  • missveronica

    (@missveronicatv)

    @celinehag

    You can try this code snippet.

    Replace with your form_id’s:

        $new_form_id = '9999';
        $old_form_id = '1111';

    Make a database backup first of all.

    The code snippet will execute when you display an UM page without any output from the code snippet. Time to load the page depends on how many users you have in your DB. When you got the page displayed remove the code snippet and look at your usermeta table.

    add_action( "init", "um_update_usermeta_form_id" );
    
    function um_update_usermeta_form_id() {
    
        global $wpdb;
        $new_form_id = '9999';
        $old_form_id = '1111';
    
        $wpdb->query( "UPDATE {$wpdb->usermeta} 
                            SET meta_value = '{$new_form_id}' 
                          WHERE meta_key = 'form_id' 
                            AND meta_value = '{$old_form_id}'" );
    
        $users = get_users();
        foreach ( $users as $user ) {
            um_fetch_user( $user->ID );
            $submitted = maybe_unserialize( um_user( 'submitted' ));
            if( !empty( $submitted ) && isset( $submitted['form_id'] )) {
                if( $submitted['form_id'] == $old_form_id ) {
                    $submitted['form_id'] = $new_form_id;
                    update_user_meta( $user->ID, 'submitted', $submitted );
                }
            }
            UM()->user()->remove_cache( $user->ID );        
        }
    }
    Thread Starter celinehag

    (@celinehag)

    @missveronicatv

    Nice little piece of code! Works perfectly! You actually saved my day with this. Thank you so much.

    I was getting pretty close to a solution yesterday myself. Only thing left was to iterate through the users. I like yours better, though because it let’s me specify the old_form_id.

           $user_id = x;
    	$new_form_id = y;
    	
    	$submitted_field = get_user_meta( $user_id, 'submitted', true );
    	$submitted_field['form_id'] = $new_form_id;
    	
        $toupdate= array(
            'form_id' => $new_form_id;,
    		'submitted' => $submitted_field	
        );
    
        foreach( $toupdate as $k => $new_val ){ 
          update_user_meta($user_id, $k, $new_val,false);
        }
    
        UM()->user()->remove_cache( $user_id );
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Update form_id in wp_usermeta table’ is closed to new replies.