Skip Navigation

[Resolved] CRED user form to include WPUser Avatar field

This thread is resolved. Here is a description of the problem and solution.

Problem:
How to update user avatar field using CRED form

Solution:
The correct meta key to update the user avatar is "wp_user_avatar". You can use CRED hook "cred_save_data" to update the user avatar.

you can find proposed solution, in this case, with the following reply:
=> https://toolset.com/forums/topic/cred-user-form-to-include-wpuser-avatar-field/#post-622519

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

This support ticket is created 6 years, 10 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 9 replies, has 3 voices.

Last updated by andreaC-10 6 years, 9 months ago.

Assisted by: Minesh.

Author
Posts
#621931

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.

I tried following the instruction here: https://toolset.com/forums/topic/passing-a-value-from-a-cred-form-to-another-field/ but nothing seems to be saving.

Here are the relevant lines of my code:

add_action( 'cred_save_data', 'team_save_data_action', 10, 2 );
function team_save_data_action( $post_id, $form_data ) {

if ( $form_data[ 'id' ] == 159 ) {

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);
}
}
}

#622004

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - could you please share the problem URL where you added the CRED form?

#622270

Of course! Sorry!
hidden link
You'll need to be logged in to view the form

#622430

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)

#622519

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

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.

#625402

Thank you it works perfectly!

#625512

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Glad to know that solution I shared with you worked perfectly for you and help you to resolve your issue.

As your issue is resolved - Could you please kindly mark resolve this ticket 🙂

#625771

Complete - thank you again

#1546925

Hi guys,

Trying to apply this script to my CRED Form

I created a user field as image with slug "immagine-del-profilo"

but can't get the $imageid from the query $wpdb->get_var()

$imageid = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE guid=%s", $_POST['wpcf-immagine-del-profilo']) );

the value of "$_POST['wpcf-immagine-del-profilo']" return only the filename, not the full url of image and the query return null.

Any suggestion?

Thank you

#1548591