I have a CRED New User form that, on submit, creates both the new WP User and a new Custom Post of type 'team'.
I am using the WP User Avatar plugin which stores a Media Post ID in the wp_usermeta table.
I need a field in my CRED form to allow the uploading of an Avatar image. To do this I tried creating a custom User field for the image upload, with the intention of copying the value and updating the user meta with it but I can't get it to work.
if (isset($_POST['wpcf-user-image']))
{
// get the ID of the uploaded image
$imageid = url_to_postid( $_POST[ 'wpcf-user-image'] );
// add it to saved post meta
add_user_meta($post_id, '_wp_user_avatar', $imageid, true);
}
}
}
Please note that I am also having SMTP issues so no emails are sending (just incase you're wondering why you don't get a welcome email when testing the form)
Well - here is the final code that works perfectly - the issue is you were using wrong user meta key _wp_user_avatar , the correct meta key is wp_user_avatar (without first letter as underscore _ ):
And another issue is wrong function and I query the guid and get the post id based on guid.
// 2. Get User Avatar from custom field
// didn't work --> $imageid = url_to_postid( $_POST[ 'wpcf-user-image'] );
// didn't work --> update_user_meta( $_POST, 'wpcf-user-image-id', $imageid );
// didn't work --> update_user_meta( $_POST, 'wp_user_avatar', $imageid );
if (isset($_POST['wpcf-user-image']))
{
// get the ID of the uploaded image
//$imageid = url_to_postid( $_POST[ 'wpcf-user-image'] );
global $wpdb;
$imageid = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE guid=%s",$_POST[ 'wpcf-user-image']) );
// add it to saved post meta
update_user_meta($post_id, 'wp_user_avatar', $imageid);
}
I can see its working - could you please confirm.
Also - As your original issue is resolved, May I kindly ask you to open a new ticket for your each new question. This will help other users searching on the forum.