1

I checked justin tadlocks custom profile field tutorial and wanted to add the field in the custom dashboard widget but it seems it's not working. I want to show all the user information in this widget.

All that I want to do is add the custom information from the profile field to this dashboard widget and it seems to be not working.

My aim here to to get all author information and show it on this dashboard. Any ideas how to achieve this task?

function example_dashboard_widget_function() {
// Display whatever it is you want to show
the_author_meta();
 } 

// Create the function use in the action hook
function example_add_dashboard_widgets() {
wp_add_dashboard_widget('example_dashboard_widget', 'User Profile',    'example_dashboard_widget_function');
}
// Hoook into the 'wp_dashboard_setup' action to register our otherfunctions
add_action('wp_dashboard_setup', 'example_add_dashboard_widgets' );
4
  • possible duplicate of Custom Dashboard Widget with Custom Notification from Admin, How to?
    – kaiser
    Jul 29, 2012 at 12:23
  • Normally this should be (because of the wrong usage of the_author_meta()), just be closed as "too localized", as the question just comes out of the lack of a) reading the documentation and b) trying to fix it, but as I just stumbled upon something with the same target and a working solution, I close vote "duplicate".
    – kaiser
    Jul 29, 2012 at 12:24
  • I tried reading the documentation. I didn't find a way to fetch all user information fields. So used the_author_meta. I thought it could even fetch the custom fields too.
    – Ryu
    Jul 29, 2012 at 12:26
  • Take a look at my answer ↓ ...
    – kaiser
    Jul 30, 2012 at 14:01

1 Answer 1

1

There's get_the_author_meta() for such a task. (get_* functions normally don't echo/print the output - hence the name).

// Both values are *optional*
get_the_author_meta( $field, $user_id );

Normally it's only meant to be used inside a loop to get the data of the posts author, therefore it internally uses global $authordata;.

But, you can also throw in the data of the current user, as user data is always the same (same tables, same data).

global $current_user;

// Test to see what you get:
echo '<pre>'.var_export( $current_user, true ).'</pre>';

get_the_author_meta( '', $current_user->ID );

// OR: simply, without the global
get_the_author_meta( '', get_current_user_id() );

Now the only thing left is to call the meta for each field, using get_user_meta(), which is pretty much equal (for this task) to get_the_author_meta();.

$user_meta = get_user_meta( get_current_user_id() );
// OR: Use the function, where get_user_meta() is the API wrapper for
$user_meta = get_meta_data( 'user', get_current_user_id(), '', true );

Then just loop through it:

foreach ( $user_meta as $meta_data )
    echo '<pre>'.var_export( $meta_data, true ).'</pre>';

Your Answer

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

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