Home Forums Front-end Issues How to show user-data on frontend for 'current-user' instead of userid


How to show user-data on frontend for 'current-user' instead of userid

  • I am completly new to ACF as of yesterday, but ive been developing for WP/WC for years. I cant believe i never tried ACF before yesterday. Anyways, I am trying to show an acf field on the front end, for a user data field i created. Here is my scenario.

    I want 3 fields in a user profile only an admin can edit. The 3 fields will have numbers in them and each user will be different data.

    I then need to display these 3 number fields on the front end in the “my account” section under its own tab. So i have the new tab under my account setup and i am trying to display the data of the 3 fields but all of the documentation for ACF says that i need to define the user_id when calling the field in the php code, but i need it to get the user_id from the currently logged in user instead of me manually entering it in the code, so that any user that visits the tab in my account, it shows their data.

    Is this achievable with acf? If so, how?

    // get the current user id
    $user_id = get_current_user_id();
    // get a field from the user
    $value = get_field('field_name', 'user_'.$user_id');
  • This code did not work. I think i am not setting up the custom field properly. What settings should be set, to what, for the above code to work? I have it set to “Text” for the type and conditions to all users but it wont work. I even have Elementor Pro installed, and when i put a text area into a page, and then use the Dynamic feature for the text, i select ACF Custom Field as the dynamic content, and then the fields show up there and any of them that i select, none of them show on the front end or in the elementor editor. So im beginning to think that my custom field is not set up properly.

  • You have lost me a bit.

    When you set up the fields the location of the field group should be:

    User Form is equal to Add/Edit and
    Current User Role is equal to Administrator

    This will show the field group on the WP user profile page and then the above code should work wherever you can use PHP to get an show the fields. The field settings should not matter.

  • I was able to figure it out with Elementor, and changing some settings in the Custom Field. My problem was i was using Elementor > Text Area Widget > Dynamic Tags > ACF Field > (select my field from dropdown).

    What i needed to do, to pull the data from the users profile, was this: Elementor > Text Area Widget > Dynamic Tags > Site – User Info > Field: (dropdown) User Meta > Meta Key (type field name into this box)

    This successfully pulls the data from the user profile that the custom fields are for. I was not aware of this, and to be honest, this is not mentioned anywhere in the ACF or Elementor Pro documentation, i had to figure this out on my own and i know for a fact there are TONS of other ACF/Elementor users who are trying to pull data from the custom user fields and display it on the front end of their site.

    But my “issue” is now solved.

  • There are tons of other elementor users trying to use all kinds of data from all kinds of places. I am not much help with elementor because I just code templates in PHP. We could use someone that knows their way around elementor helping others, but that’s never going to be me.

  • Hey John, sure I would be able to help. I know a really good amount about Elementor, but i am just getting into ACF and still learning. I know a small amount of PHP, i am extremely knowledgable in CSS, HTML and JS. Still learning PHP but i can do more small scale stuff. I have been designing sites and coding since 1999 and i also am a graphic designer and video editor, ive been doing graphics in Adobe since 1999 and just last year i started to learn Final Cut Pro X.

    Anyways, i am always down to help if i know how to fix the persons issue. Ill stick around and check on new posts every once and awhile.

  • This code may help you:

    // get the current user id
    $id = get_current_user_id();
    // get a field from the user
    $variable = get_field('field_name', "user_{$id}");
    <!-- Display the field from the user -->
    <p><?php the_field('field_name', "user_{$id}"); ?></p>
  • I just registered and dropped in to say thank God I found this thread among piles of documentation and video tutorials regarding how to actually display custom user fields of the current logged in user – specifically using Elementor.

    Major props to user BlackForest for pointing this out – because the first obvious method of selecting through the Elementor menus is what most people would expect to use, and not the latter as pointed out. Thank you.

  • I have the same problem. Is it enough to put @domingoapineiro ‘s code in the functions.php file of the theme? Or should it be inserted into the Elementor page where I want it to appear? Because Elementor won’t let me insert PHP code widgets.

Viewing 10 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic.