Skip Navigation

[Résolu] Render User field with echo types_render_usermeta(« staff-role »);

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem: I would like to display a custom field from the current logged-in User's profile in my PHP template, but the Types Field API doesn't seem to be working correctly.

Solution: Verify the User field contains data for the current User.

Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/functions/

This support ticket is created Il y a 3 années et 11 mois. 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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 4 réponses, has 2 voix.

Last updated by Oniromancer Il y a 3 années et 11 mois.

Assisted by: Christian Cox.

Auteur
Publications
#1591421
CleanShot 2020-04-19 at 11.29.12@2x.png

Hi

as per https://toolset.com/documentation/customizing-sites-using-php/creating-custom-user-profiles/

function pewpew(){
echo types_render_usermeta("staff-role");
}
//using the astra theme
add_action('astra_entry_content_before','pewpew');

doesn't display anything.

#1591947

Hello, as a quick test, can you confirm this function renders some static content before the custom field? For example:

function pewpew(){
echo 'static content test, followed by usermeta: ' . types_render_usermeta("staff-role");
}

You should see "static content test, followed by usermeta: " at the beginning of the post content. It seems to work in my local test, but I'm not 100% sure I understand when this hook should be implemented. In my local tests, I see the static content and MY usermeta field rendered before the post content of a custom Page created by me. If this isn't what you see, please tell me more about how you added this custom code, and whose custom field information you expect to see - the current logged-in User, the author of the current post, an arbitrary User, the author whose author archive is currently being displayed, etc...

#1592075
CleanShot 2020-04-20 at 01.57.15@2x.png

hi,

- You should see "static content test, followed by usermeta: " at the beginning of the post content.
Correct, that is what I see. I do not see the metadata.

The content is displayed before the post content of a single post created by me.

- how you added this custom code
Using Types, field type select, with exactly the same data of the tutorial.

- and whose custom field information you expect to see
I expect to see the user custom data staff-role, which is a single line (I am expecting to see the description and the placeholder). If you could expand on this.

- the current logged-in User, the author of the current post,
The current logged-in user is an admin and also author of the post

- the author whose author archive is currently being displayed,
I don't understand this part.

I think the problem is about how to correctly handle the content of the expected result, if you could explain.
If I understand correctly I have to create the field in HTML on the frontend (in this case a single line and a checkbox).
I then have to send a POST request on ajax_admin to retrieve the content of the metadata back on the frontend.
Is this the correct way to retrieve the expected data and do you have a short tutorial I could follow.
I was under the impression that Toolset was taking care of this server/client communication.

I also have tried this code:
$toolset1 = get_post_meta(get_the_ID(), 'wpcf-this', true);
echo '</br>and again with userfield: '.types_render_field( 'this').'</br>and this: '.$toolset1;

They are both working, displaying a double true (1) value for "this" checkbox which is validated in the post backend.

Best regards

#1592121
user-profile.png
edit-field.png
single-book.png

- You should see "static content test, followed by usermeta: " at the beginning of the post content.
Correct, that is what I see. I do not see the metadata.

Please go to wp-admin > Users > Your Profile and show me a screenshot of the staff-role User field. What value is selected for your User? You can see mine in user-profile.png - I have "Option title 2" selected.

Using Types, field type select, with exactly the same data of the tutorial.
The tutorial doesn't include any information about the options and values in the select field, so please provide more information about the select field and its options. Something like your screenshot CleanShot 2020-04-20 at 01.57.15@2x.png, but please show the information for the Staff role select field instead. You can see mine in edit-field.png

- the author whose author archive is currently being displayed,
I don't understand this part.

Sorry for the confusion, let me try to explain. An author archive is an archive of the posts created by one User. For example, hidden link will hold an archive of posts created by User joe-smith. I was asking if you want show the staff-role custom field for joe-smith in the author archive for joe-smith.

I expect to see the user custom data staff-role, which is a single line (I am expecting to see the description and the placeholder). If you could expand on this.
Staff role is a select field, not a single line. I think this was just an oversight, though, no problem.

If I understand correctly I have to create the field in HTML on the frontend (in this case a single line and a checkbox).
I then have to send a POST request on ajax_admin to retrieve the content of the metadata back on the frontend.

I'm not really sure what all this is for. There is no need for a POST request to display a User field on the front-end. There's no need to create a field in HTML on the front-end. In your PHP template you can simply echo types_render_usermeta, and the selected option should be displayed. In single-cpt.php, for example:

// ... get_header and other stuff before this
  echo '<h2>this is my single-book.php template</h2>';
  echo types_render_usermeta('user-select-1');
  echo '<br />this is after usermeta';

I also added in my Toolset Content Template for Books just before wpv-items-found:

<h2>This is in my Toolset-based content template applied to single Book posts</h2>
User select 1: [types usermeta='user-select-1'][/types]<br />

The result can be seen in attached single-book.png. You can see the selected option text "Option title 2" is displayed by types_render_usermeta and also by the Types field shortcode.

I also have tried this code:
$toolset1 = get_post_meta(get_the_ID(), 'wpcf-this', true);
echo '</br>and again with userfield: '.types_render_field( 'this').'</br>and this: '.$toolset1;

The function get_post_meta is for getting custom field values from a post of some kind, not from a User profile. You cannot access User fields with get_post_meta. You must use a different function, like get_user_meta: https://developer.wordpress.org/reference/functions/get_user_meta/
The types_render_usermeta function is similar to get_user_meta, but it applies additional formatting based on the options you provide in the arguments array. If you use get_user_meta to get this User field, the result will be 2, not Option title 2. If you use types_render_usermeta, you can get Option title 2. I hope this helps clarify.

#1592527

My issue is resolved now. Thank you!

The problem was that I forgot to insert the current user metadata and I noticed once I tried with other users.

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