• I have done three functions to add, delete and update user meta.
    For example to delete some user data first I have to delete user_meta stored and save it again, otherwise I get arrays inside of arrays …
    Also I have to use some strange code as $user_meta[0] to get user meta correctly.
    My question is: This is is the way to do it? Because it seems to work, but is very ugly code for me.

    // function delete

    $user_meta_backup = array();
    $user_meta = get_user_meta( $user_id, 'my_data', false );
    $user_meta = $user_meta[0]; // why ???
    foreach( $user_meta as $key => $value)
                { if ($key != $user_id ) 
                    $user_meta_backup[ $key] = $value;
                }
    $delete = delete_user_meta( $user_id, 'my_data', false );         
    $result = add_user_meta( $user_id, 'my_data', $user_meta_backup);  
    

    // function add meta

    $user_data = get_user_meta( $user_id, 'my_data', true);</strong>
    if ( !is_array( $user_data))
        $user_data = array();
        $user_data[$user_id] = [ 'user_id'=>$user_id, 'user_name' => $user_name, 'user_email' => $user_email];
    $result = update_user_meta( $user_id, 'my_data', $user_data); 
    

    // function list user meta

    $user_meta = get_user_meta( $user_id, 'my_data' );
    $user_meta = $user_meta[0]; // why ???
    // then list here ...
    
    • This topic was modified 4 years, 9 months ago by capbussat.
    • This topic was modified 4 years, 9 months ago by capbussat.
    • This topic was modified 4 years, 9 months ago by capbussat.
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘get_user_meta with an array of data’ is closed to new replies.