1

New to wordpress development. I was trying to solve the problem of adding extra fields to my wordpress logged in subscriber user profile area, and also echoing the field values in my custom page template html. I have added 3 extra fields to the profile with a ciemyFieldValue plugin. My main issue is retrieving the value of these fields in my custom dashboard page template, like so: $total = get_cimyFieldValue( get_query_var( 'subscriber' ), 'total' );., it returns (Array) instead, how can I get out the extra field value from the $total array?

5
  • 1
    hey there. i never worked with the plugin you mention and many answer here go: oh you have plugin issue? go ask the author. i would do the same. but then i saw that the plugin got its last update 4 years ago, so it looks like it is unmaintained. please checkout this plugin, it can add custom fields to everything, (users, posts, pages, attachments..) is easy to use, well documented and is very well maintained. other than that you ask on how to display the contents of an array and that is a simple php operation: print_r($total, true);
    – honk31
    Commented Dec 27, 2020 at 8:38
  • Thanks for your prompt response @honk31, With this plugin you sent, can I retrieve the values of the custom fields easily on my html page for logged in users? Commented Dec 27, 2020 at 8:46
  • of course! like i said before, check out the documentation.. the only problem i see here is: how to get the user ID. try it out, maybe you'll find something here on stackoverflow, if not: ask :)
    – honk31
    Commented Dec 27, 2020 at 8:52
  • You can get the user id using WP function developer.wordpress.org/reference/functions/get_current_user_id
    – Q Studio
    Commented Dec 27, 2020 at 9:17
  • Hi guys, I was able to echo the user_id variable on my custom page. How can I get user_id as a parameter in $variable = get_field('field_name', 'user_1');. @honk31 Assuming i am utilizing the get_field code snippet Commented Dec 27, 2020 at 11:18

1 Answer 1

1

i'm using this answer as a playground for helping you out here. better, than in the comment section. for anybody else stumbling about the same question: i recommended ACF as a plugin for custom fields, as it is well maintained and can add custom fields to anything inside wordpress (posts, pages, attachments, taxonomies...)

to get the current user and their fields, it would go something like this

function so380508_get_user_total(){

    $user_id = get_current_user_id();

    // @todo - validate we have a user ID

    $user_total = get_field('total', 'user_' . $user_id);

    // @todo - validate field data returned - if an array, get required data in printable format

    // @todo - format markup and return

    echo '<pre>' . print_r($user_total, 1) . '</pre>';

}

and inside your template file you need to call so380508_get_user_total();

of course you need to create the field 'total' as a custom field for users. and for testing purposes create a user, fill that field with a random value, login as that user and see, if you get, what you are looking for.

2
  • 3
    Best to wrap this in a function and put in placeholders for sanity and validation checks, as this is a learning exercise.
    – Q Studio
    Commented Dec 28, 2020 at 11:00
  • 1
    Also, note that get_field is an ACF function, not standard WP, so you should check if it is available.
    – Q Studio
    Commented Dec 29, 2020 at 9:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.