Support

Account

Home Forums Add-ons Repeater Field Select a specific value from an image object in a repeater field

Solved

Select a specific value from an image object in a repeater field

  • I have a images in a repeater field added to the user settings page so a user can have a choice of avatar. Once they make the choice the ID of the chosen image object is added to their user meta.

    Is there a way to use that image object id to select that specific images url for display as their avatar?

  • Hi @eggbeater

    I believe you can do it like this:

    function example_callback( $avatar, $id_or_email, $size, $default, $alt, $args ) {
        $user_image = get_field('user_image_field_name', 'user_1');
        $gravatar_url = str_replace('&','&',$args['url']);
        $avatar = str_replace($gravatar_url, $user_image['sizes']['thumbnail'] , $avatar);
        return $avatar;
    }
    add_filter( 'get_avatar', 'example_callback', 20, 6 );

    Keep in mind that that code is an example for an image field, not a repeater field. Also, please change the “user_1” parameter. This page should give you more idea about it: http://www.advancedcustomfields.com/resources/how-to-get-values-from-a-user/.

    I hope this helps.

  • I was actually able to solve this with a completely WP method.
    Since I had the user’s chosen image ID stored in the user meta I was able to get the URL using that ID

    
    <?
    // Get the file id
    $image_id = get_user_meta($userid, 'meta_field_key', true); // CHANGE TO YOUR FIELD NAME
    // Get the file size
    $image_url  = wp_get_attachment_image_src( $image_id, 'thumbnail' ); // Set image size by name
    // Get the file url
    $avatar_url = $image_url[0];
    ?>
    
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Select a specific value from an image object in a repeater field’ is closed to new replies.