The Ultimate Guide to Display Custom Fields for User Profiles in WordPress

The template function get_field and the_field will work the same as it works for the posts. The only change is in the second parameter of the function. It will be user_{user_id} instead of post_id. The custom fields for the user are saved in the user_meta table.

The Ultimate Guide to Display Custom Fields for User Profiles in WordPress

Here is the example of displaying values of each field types of custom fields for user:

  1. Text
<?php the_field( ‘tin_number', 'user_1' ); ?> // where 1 is the user_id
  1. Checkbox
<?php the_field( ‘countries_served’, 'user_1' ); ?> 
  1. Radio button
<?php the_field( ‘countries_served’, 'user_1' ); ?> 
  1. Select
<?php the_field( ‘countries_served’, 'user_1' ); ?> 
  1. Image

The Image field will return either an array, a string or an integer value depending on the Return Value set. 

Display Image using ID as return value

<?php $image = get_field( 'image', ‘user_1’ );

$size = 'full'; // (thumbnail, medium, large, full or custom size)

if( $image ) {

    echo wp_get_attachment_image( $image, $size );

}

Display Image using array as return value

<?php $author_id = get_the_author_meta( 'ID' );

$image = get_field( 'image', 'user_'. $author_id);

if( !empty( $image ) ): ?>

    <img src="<?php echo esc_url($image['url']); ?>" alt="<?php echo esc_attr($image['alt']); ?>" />

<?php endif; ?>
  1. Repeater

When you use the_sub_field or get_sub_field inside the have_rows loop, you do not need to use the user_id.

<?php if( have_rows( 'repeater', 'user_1' ) ): ?>

    <ul>

    <?php while( have_rows( 'repeater', 'user_1' ) ): the_row(); ?>

        <li><?php the_sub_field( 'title' ); ?></li>

    <?php endwhile; ?>

    </ul>

<?php endif; ?>
  1. Flexible content
<?php $store_user = dokan()->vendor->get(get_query_var('author'));

$store_user_id = $store_user->get_id(); 

$user = 'user_' . $store_user_id . ''; ?>

<?php if( have_rows( 'user_info',  $user ) ): ?>

    <?php while( have_rows( 'user_info', $user ) ): the_row(); ?>

        <?php if( get_row_layout() ==  'user_details' ): ?>

            <?php echo "first_name"; ?>

            <?php the_sub_field( 'first_name' ); ?>

        <?php endif; ?>

    <?php endwhile; ?>

<?php endif; ?>

The other fields like link, file, google map are similar to the above field types. The custom fields for user more or less acts same as post/page fields, the only difference is how to display it. Feel free to contact us in case you have any query or wish to add anything to this blog.

Also more read like this Blog to Our site https://krazyplugins.com/