Skip Navigation

[Resolved] Userform native WordPress profile picture

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

Problem: I would like to use a Form to update a custom post. When the Form is submitted, I would like to automatically set a User custom field image using the Featured Image from the Form.

Solution: Use custom code and the Forms API:

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==83) //can i add more than one form?
    {
        $image_value = get_the_post_thumbnail_url( $post_id, "full"); // full size, change for a different size

        $user = wp_get_current_user();
        $user_id = $user->ID;
        update_user_meta($user_id, 'wpcf-profilbild', $image_value);
    }
}

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
https://developer.wordpress.org/reference/functions/get_the_post_thumbnail_url/

This support ticket is created 4 years, 5 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 9 replies, has 2 voices.

Last updated by SteffenM1628 4 years, 5 months ago.

Assisted by: Christian Cox.

Author
Posts
#1383467

Tell us what you are trying to do?

Hey there, i try to build a userform in which a user can use the standard wordpress profilpicture.

how can i do this. i found nothing.

Cheers

#1383521

Hi, as you may know, the WordPress User profile picture is managed with Gravatar, which is an external service. Toolset Forms is not designed to interact with this external service, but you could use a custom image field instead and manage that with Forms. Another ticket discusses this option here: https://toolset.com/forums/topic/insert-custom-photo-to-wp-profile-picture/

Here is the information for the external Gravatar service: https://en.gravatar.com/

#1384097

hey Christian,

thanks for your reply.
it works with the types fields but the link you posted would be the perfect solution for me.
please check my next post
Cheers

#1384101

the solution from the link is not exactly clear for me:
You can use a Custom Image User Field instead.

1. Create a user Field (Image)

2. Create a Post Field (image)

3. Create a CRED Form to create (or update) a Post

4. Insert this code (adapt it) to your functions.php:

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==44)
    {
        $image_value = get_post_meta($post_id, 'wpcf-image', true);
        $user = wp_get_current_user();
        $user_id = $user->ID;
        update_user_meta($user_id, 'wpcf-image-user', $image_value);
    }
}

Why do i need two image fields? One a post one a user.
I only have a user types image field. does this work as well with only a user upload-image?

cheers

#1385213

The other ticket involves a separate custom post type, "member-profile", where the User is managing this image. In your case, there is no "member-profile" post so there is no need for a 2nd image custom field, and there is no need for any custom code. Your Users will manage their image in an Edit User Form, and there will be no Edit Post Form.

I hope this helps, let me know if this is confusing and I can give more guidance.

#1386339

My issue is resolved now. Thank you!

#1386355

Hey Chris,

at first i thought i had it but now... 🙁

From my Posttype it is the featured-image-field which i want to copy to a user field.
the userfield is called = profilbild
the id of the form is 83.

but i don´t get this running!

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==83) //can i add more than one form?
    {
        $image_value = get_post_meta($post_id, 'what is the featured image id?', true);
        $user = wp_get_current_user();
        $user_id = $user->ID;
        update_user_meta($user_id, 'wpcf-profilbild', $image_value);
    }
}

Thanks a lot.

#1386405

The featured image is a bit different, because only the image ID is stored as "_thumbnail_id". It's better to use the WP API get_the_post_thumbnail_url:

$image_value = get_the_post_thumbnail_url( $post_id, "full"); // full size, change for a different size

In this code I used the "full" size but you could use "post-thumbnail" or any other registered size.
https://developer.wordpress.org/reference/functions/get_the_post_thumbnail_url/

#1387309

thanks a lot chris!! Worked fine for me

#1387317

Great!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.