0

So I have a Wordpress website, and I want to be able to display the current logged in user's data on a page. Previously, I had been able to find a snippet of code that would go go in functions.php and I would be able to use a shortcode like [currentuser_(specific_metadata_tag)] and it would display it. I cannot find the stack exchange question that had this code. Is it possible for anyone to repost the link to that question, or maybe give me a piece of code to implement into the functions file so I can use a shortcode to access current user data? I think it should be fairly straightforward, the function would just get current user id then look under a specific metadata and pull data, then the short code would just display.

In any sense, I am not fully familiar with coding in PHP and so if I can receive any help, I would really appreciate it!

0

You can try below snippet of code. i have develop shortcode you can take reference from it.

function current_user_data_func( $atts ) {

    extract( shortcode_atts( array(
        'metakey' => '',
        'user_field' => '',
    ), $atts) );
     ob_start();
    if(is_user_logged_in())
    {
        $current_user = wp_get_current_user();

        $c_user_id = $current_user->ID;
        if(!empty($metakey))
        {
            $value = get_user_meta( $c_user_id, $metakey, true); 
            echo $value;
        }

        if(!empty($user_field))
        {
            echo $current_user->{"$user_field"}; 

        }

    }
    else
    {
        echo "User Not Logged In";
    }
  return ob_get_clean();
}
add_shortcode( 'current_user_data', 'current_user_data_func' );

How to use :

User_fields : 
  user_login
  user_email
  user_firstname
  user_lastname
  display_name

# In template
echo do_shortcode( '[current_user_data metakey="nickname"]' );
echo do_shortcode( '[current_user_data user_field="user_email"]' );

#in content 

[current_user_data metakey="first_name"]
[current_user_data user_field="user_email"]

meta key return only single value if you have stored array value in meta key then you have to remove 'true' from get_user_meta( $c_user_id, $metakey, true); and get value from that array variable $value.

Let me know if this helps to you!

| improve this answer | |
  • Does this work for other user data? I have created some of my own metadata like user_answerKey. If I use [currentuser_user_data metakey="user_answerKey] will it work and display data of that metakey? – FAQ Tutoring Jun 27 '19 at 10:28
  • yes it will work. what kind of data are you storing in 'user_answerKey' key ? array or just string ? if not array then you can get value of particular meta key. try and let me know if this works for you. – Chetan Vaghela Jun 27 '19 at 10:33
  • Yes, it is a specific value (or as you say "string"), not an array. I will test it out and let you know if it works. – FAQ Tutoring Jun 27 '19 at 11:26
  • So @Chetan Vaghela the code almost works. There is a bug in it where even if the shortcode is not at the top of the page, the data displays at the top of the page. So persay the shortcode for the first name was NOT at the top of the wordpress page, however the first name still displays at the top of the page. – FAQ Tutoring Jun 27 '19 at 11:42
  • Did you used in HTML in Shortcode Function ? okay. i have updated my answer. i have added ob_start(); and return ob_get_clean(); in function. check is out and let me know if there any issue. – Chetan Vaghela Jun 27 '19 at 12:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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