Opens in a new tab
16th Mar '26
/
0 comments

Custom Image Field Avatar in WordPress

Sometimes you may want to not use the built-in avatar feature in WordPress and instead use a image-type custom field.

Consider a “User fields” field group created with Meta Box having an Image Upload type of field with an ID of avatar.

Users (that can edit their user profile) can select an image as their avatar:

Assuming that you are using Bricks theme for your WordPress site, you now want to add an Image element in the single post template and map it to a custom dynamic data tag that returns the ID of the image set for the user that has authored or published the post.

Meta Box provides a handy rwmb_meta() function for getting field values.

Step 1

In your single post template, add a Code element having:

<?php

$img = rwmb_meta( 'avatar', [ 'object_type' => 'user' ], get_the_author_meta( 'ID' ) );

print( "<pre>" . print_r( $img, true ) . "</pre>" );
  
?>

View any single post on the front end published by a user that has the custom avatar Meta Box field set.

It should be something like:

Array
(
    [542] => Array
        (
            [width] => 150
            [height] => 150
            [file] => 2026/03/frances-harper.jpg
            [filesize] => 15373
            [sizes] => Array
                (
                    [thumbnail] => Array
                        (
                            [file] => frances-harper-150x150.jpg
                            [width] => 150
                            [height] => 150
                            [mime-type] => image/jpeg
                            [filesize] => 6535
                            [url] => https://devsite.local/wp-content/uploads/2026/03/frances-harper-150x150.jpg
                        )

                    [woocommerce_gallery_thumbnail] => Array
                        (
                            [file] => frances-harper-100x100.jpg
                            [width] => 100
                            [height] => 100
                            [mime-type] => image/jpeg
                            [filesize] => 3970
                            [url] => https://devsite.local/wp-content/uploads/2026/03/frances-harper-100x100.jpg
                        )

                )

            [image_meta] => Array
                (
                    [aperture] => 0
                    [credit] => 
                    [camera] => 
                    [caption] => 
                    [created_timestamp] => 0
                    [copyright] => 
                    [focal_length] => 0
                    [iso] => 0
                    [shutter_speed] => 0
                    [title] => 
                    [orientation] => 0
                    [keywords] => Array
                        (
                        )

                )

            [ID] => 542
            [name] => frances-harper.jpg
            [path] => /Users/sridharkatakam/Sites/devsite/app/public/wp-content/uploads/2026/03/frances-harper.jpg
            [url] => https://devsite.local/wp-content/uploads/2026/03/frances-harper-150x150.jpg
            [full_url] => https://devsite.local/wp-content/uploads/2026/03/frances-harper.jpg
            [title] => frances-harper
            [caption] => 
            [description] => 
            [alt] => 
            [srcset] => https://devsite.local/wp-content/uploads/2026/03/frances-harper-150x150.jpg 150w, https://devsite.local/wp-content/uploads/2026/03/frances-harper-100x100.jpg 100w, https://devsite.local/wp-content/uploads/2026/03/frances-harper.jpg 250w
        )

)

As can be seen, the array has a single element with the key of the image ID (542, in this example).

Since we want to be able to set the image size in the Image element’s controls, we need to pull this ID rather than the image URL.

Step 2

Add the following in child theme‘s functions.php (w/o the opening PHP tag) or a code snippets plugin:

<?php

/**
 * Get the attachment ID of the author's avatar from Meta Box.
 *
 * @param int $user_id The user ID. Defaults to the current post author.
 * @return int The attachment ID, or 0 if not set.
 */
function brickslabs_get_author_avatar_id( $user_id = 0 ) {
	if ( ! $user_id ) {
		$user_id = get_the_author_meta( 'ID' );
	}

	$images = rwmb_meta( 'avatar', array( 'object_type' => 'user' ), $user_id );

	if ( empty( $images ) ) {
		return 0;
	}

	return (int) key( $images );
}

key() function of PHP returns the index element of the current array position.

Step 3

Next, whitelist the brickslabs_get_author_avatar_id function.

Ex.:

<?php 

add_filter( 'bricks/code/echo_function_names', function() {
  return [
    'brickslabs_get_author_avatar_id'
  ];
} );

You should also add other functions (native or custom) being used in your Bricks instance besides brickslabs_get_author_avatar_id. This can be checked at Bricks → Settings → Custom code by clicking the Code review button.

More info on whitelisting can be found here.

Step 4

Add an Image element in your single post Bricks template and set its source to:

{echo:brickslabs_get_author_avatar_id}

Select your desired image size.

The Code element from Step 1 can now be deleted.

Get access to all 633 Bricks code tutorials with BricksLabs Pro

Leave the first comment

 

Related Tutorials..

Pro
Dynamic Post-specific Templates in Bricks using Meta Box/ACF Select Field

Dynamic Post-specific Templates in Bricks using Meta Box/ACF Select Field

Update on 23 Aug 2023: Added steps for ACF. Bricks builder v1.8.4 introduced an pretty useful filter called bricks/active_templates that flew under the radar. This…
Pro
Dynamic Accordion in WordPress using Meta Box and Alpine.js

Dynamic Accordion in WordPress using Meta Box and Alpine.js

This Pro tutorial provides the steps to implement an accordion using Alpine.js in WordPress that pulls the values of sub fields of a Meta Box…
Pro
Filtering posts by a group field’s true / false field sub field value with ACF or Meta Box in Bricks

Filtering posts by a group field’s true / false field sub field value with ACF or Meta Box in Bricks

This Pro tutorial covers how posts of a Bricks query loop can be filtered to show only the ones whose true / false or checkbox…
Categories:
Tags:
Pro
Meta Box Group Subfield Text’s Slug as Query Loop Item ID in Bricks

Meta Box Group Subfield Text’s Slug as Query Loop Item ID in Bricks

In the BricksLabs Facebook group a user asks: I have a metaBox custom field called 'section_title' inside a clonable metabox group called 'about_section' How can…
Categories:
Tags:
Meta Box Category Colors in Single Posts

Meta Box Category Colors in Single Posts

Looking to use Meta Box Color Picker fields' values in a single post Bricks template based on the post category? This tutorial shows how. Note:…
Categories:
Tags:
Filtering Bricks Posts Query Loop by Meta Box Group’s Radio Subfield Value

Filtering Bricks Posts Query Loop by Meta Box Group’s Radio Subfield Value

In the past we showed how a Bricks posts query loop can be filtered by Meta Box group's subfield of types text/number and Datepicker here.…
Categories: