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?
1 Answer
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.
-
3Best to wrap this in a function and put in placeholders for sanity and validation checks, as this is a learning exercise.– Q StudioCommented Dec 28, 2020 at 11:00
-
1Also, note that get_field is an ACF function, not standard WP, so you should check if it is available.– Q StudioCommented Dec 29, 2020 at 9:16
print_r($total, true);
$variable = get_field('field_name', 'user_1');
. @honk31 Assuming i am utilizing the get_field code snippet