0

I need to display on some specific pages some selected user meta data (not all), for some users.

On the Wordpress Codex page for the get_user_meta() function we have this example (user_id = 9):

<?php $all_meta_for_user = get_user_meta( 9 ); print_r( $all_meta_for_user ); ?> 

The results of this example:

Array ( [first_name] => Array ( [0] => Tom ) [last_name] => Array ( [0] => Auger) [nickname] => Array ( [0] => tomauger ) [description] => etc.... ) 

And a Note (shortened):

... you may want to run a simple array_map() on the results of get_user_meta() in order to take only the first index of each result (this emulating what the $single argument does when $key is provided:

if( $all_meta_for_user = get_user_meta( $user_id ) ) $all_meta_for_user = array_map( function( $a ){ return $a[0]; }, get_user_meta( $user_id ) ); print_r( $all_meta_for_user ); 

The result:

Array ( [first_name] => Tom [last_name] => Auger [nickname] => tomauger [description] => etc.... ) 

My question is:

How to put this in a function that will display, for example, the user last and first names with a shortcode (but really I need more user meta data)?

What I mean? On a specific page we put a shortcode with the user ID and the related function must return the meta data for each selected key of this user. Is this possible? Can anyone help me?

8
  • Are you asking how to convert the information in an array into formatted HTML? That is a pure PHP question and will be off topic here, per the faq Commented Jan 20, 2013 at 0:11
  • @faq – s_ha_dum No, this is simple. I need a Wordpress specific function and a shortcode for that function. Commented Jan 20, 2013 at 0:14
  • Not sure what the question is? A WordPress function for what? You've pulled the user meta data. The display is up to you. That is why I thought you were asking how to convert that array into HTML. The WordPress part of building a shortcode is easy but you need to write the callback to convert that information to HTML. Is that what you are looking for? Commented Jan 20, 2013 at 0:20
  • Yes, if I understand you correctly. I want to display the user meta data (only for specific keys) on a custom user page. Commented Jan 20, 2013 at 0:26
  • If you are building a custom user page you don't need a shortcode. You can write the code into the post template. Still confused. @Bainternet posted a simple shortcode but I get the feeling that still isn't what you need, or isn't all you need. Commented Jan 20, 2013 at 0:35

2 Answers 2

3

Here is the simplest shortcode that will do the job for you

add_shortcode('USER_META', 'user_meta_shortcode_handler'); /** * User Meta Shortcode handler * usage: [USER_META user_id=1 meta="first_name"] * @param array $atts * @param string $content * @return stirng */ function user_meta_shortcode_handler($atts,$content=null){ return esc_html(get_user_meta($atts['user_id'], $atts['meta'], true)); } 

USAGE:

[USER_META user_id=1 meta="first_name"] [USER_META user_id=1 meta="last_name"] 
2
  • 1
    but maybe esc_html() or htmlspecialchars() before returning... Commented Jan 20, 2013 at 0:35
  • @webaware true! Commented Jan 20, 2013 at 0:49
0

Bellow you can see the adapted code of @Bainternet (thank you!) that I use currently. Now I can display with only one row/shortcode a big list of only custom user metadata that I need, in a custom order and formatted specifically for each type of input (text field input or textarea). Maybe the code can look better if I could get and display (HTML formatted) all the user meta data at once, excluding maybe a few, but this is too hard for me. Here is only an excerpt of the list of custom user metadata that I need to display, in reality it is longer.

add_shortcode( 'user_meta', 'user_meta_shortcode_handler' ); /** * User Meta Shortcode handler * usage: [user_meta user_id=1] * @param array $atts * @param string $content * @return string */ function user_meta_shortcode_handler( $atts, $content=null ) { $output = ""; echo '<div class="academic">'; echo '<h3>Teaching position</h3>'; echo esc_html( get_user_meta( $atts['user_id'], 'teaching_position', true ) ); echo '<h3>Scientific title</h3>'; echo esc_html( get_user_meta( $atts['user_id'], 'scientific_title', true ) ); echo '<h3>Publications</h3>'; echo wpautop( get_user_meta( $atts['user_id'], 'publications', true ) ); echo '</div><!-- .academic -->'; } 
1
  • For a better formatting of paragraphs I changed the echo wpautop(... in the way suggested here. Can this be improved? Commented Feb 2, 2013 at 23:12

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.