Advertisement
Aurangajeb

Get & show User custom field data with Shortcode

Nov 19th, 2020
1,008
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.83 KB | None | 0 0
  1. /**
  2. * Get & show User custom field data with Shortcode
  3. **/
  4.  
  5. function current_user_data_func( $atts ) {
  6.  
  7.     extract( shortcode_atts( array(
  8.         'metakey' => '',
  9.         'user_field' => '',
  10.     ), $atts) );
  11.      ob_start();
  12.     if(is_user_logged_in())
  13.     {
  14.         $current_user = wp_get_current_user();
  15.  
  16.         $c_user_id = $current_user->ID;
  17.         if(!empty($metakey))
  18.         {
  19.             $value = get_user_meta( $c_user_id, $metakey, true);
  20.             echo $value;
  21.         }
  22.  
  23.         if(!empty($user_field))
  24.         {
  25.             echo $current_user->{"$user_field"};
  26.  
  27.         }
  28.  
  29.     }
  30.     else
  31.     {
  32.         echo "User Not Logged In";
  33.     }
  34.   return ob_get_clean();
  35. }
  36. add_shortcode( 'current_user_data', 'current_user_data_func' ); // Shortcode Usage: [current_user_data metakey="uf_staffname"]
  37.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement