12% off of LTD using this coupon: TWELVEPERCENTOFF. Promo ends on 2 Dec midnight UTC.
Published on Mar 4, 2019

How to output custom user fields in Oxygen

Sridhar Katakam

In Oxygen’s Facebook group, a user asks:

I just added a custom field through ACF (free version) to the user add/edit page. It shows up and I’m able to add data (it’s a “Position” field for the position in the company). I can’t for the life of me figure out how to pull in this field via Oxygen. The post custom fields shows it but, since it’s not a post field, it’s blank.

Any ideas? Thanks in advance!

I’m using it on a template I created for blog posts. I laid it out with Author Pic, Author name, and I want author title. I removed sanitization from the bio and am coding what I want to show there but it’d be nice to find a way for ACF to work from the user profile.

We can create a function that takes in the name of a custom field linked to the user that returns the corresponding value for the current entry’s author. Then call it using the Oxygen‘s dynamic data insertion feature.

Sample screenshots:

in the profile page
value of “position” user field on the single post in parentheses

Step 1

Install and activate Advanced Custom Fields or ACF Pro.

Go to Custom Fields > Add New.

Add a new field group named say, “Additional Info”.

Let’s add a new text-type field having the label of “Position” i.e., a field name of position.

Set the field group to show if User Role is equal to All (or to specific user roles as needed).

Step 2

Install and activate Code Snippets.

Go to Snippets > Add New.

Title: Return value of post author’s custom user field

Code:

/**
 * Returns the value of supplied user field.
 * @param  string $field_name Name of the custom field created using ACF.
 * @return string             Value of user field.
 */
function sk_get_author_custom_field( $field_name ) {

	$author_id = get_the_author_meta( 'ID' );

	return get_field( $field_name, 'user_' . $author_id );

}

To output a custom user ACF field named say, position for the current post’s author we can now call the above function like so in PHP:

sk_get_author_custom_field( 'position' ).

Step 3

Edit the Template for single Posts.

Add a Text element having this text: by author (position)

We shall replace “author” with the current post’s author name and “position” with the corresponding custom field value.

a) Double click on “author” text.

Click on “Insert Data” button at the top.

Under “Author”, click on “Display Name”.

Then click “Insert” button.

b) Double click on “position” text.

Click on “Insert Data” button at the top.

Under “Advanced”, click on “PHP Function Return value”.

Function Name: sk_get_author_custom_field

Function Arguments: position

Click the “Insert” button.

Click anywhere outside the text element and you should see the position of the author that wrote that post if it has been set.

That’s it!

Reference.

tagschevron-leftchevron-rightchainangle-rightangle-upangle-downfolder-omagnifiercrossmenuchevron-downarrow-right