• Resolved howardlie

    (@howardlie)


    hello , can anyone tell me how to display current logged in user name and ID?

    eg. Someone logged in my site as user1 and have ID is 2 ,so I want to create something like “welcome , [user_name] . your ID is [user_id]”
    can anyone tell me what code to put? thanks so much ??

Viewing 11 replies - 1 through 11 (of 11 total)
  • Thread Starter howardlie

    (@howardlie)

    I want to show it inside a post/page .
    thanks

    You can with the wp function get_currentuserinfo

    Thanks zota, but I want the function’s returned value(username) assigned to a variable to be used in a plugin. not working, help.

    Try this code:

    Author: <?php the_author(); ?>
    and ID: <?php echo get_the_author_meta( 'ID' ); ?>

    If you don’t have access to the value, it’s likely a scope issue. Make sure you globalize it first:

    function wp_get_current_user() {
    global $current_user;

    get_currentuserinfo();

    return $current_user;
    }

    Using zota’s code I assigned the returned value to a variable: $variable = echo $current_user->user_id; so as to call $variable in other parts of the plugin, rather than assigning the username to the variable it was echoed all over the top part of my site.

    Did you check if you use $current_user somewhere else in your theme??
    I can`t exacly help you without seeing the full php script that you use ( frontend and backend), what i send was a direction to help you in development.

    Thanks Zota. here:
    function qbt_query_result_from_post() {
    $reg_no = $(want to set to current logged in user’s username);
    if(!empty($reg_no))
    query_posts( array(
    ‘post_type’ => ‘qbt_calls’,
    ‘meta_query’ => array(
    array(
    ‘key’ => ‘_qbt_register_no’,
    ‘value’ => $reg_no,
    )),
    ‘posts_per_page’ => 1
    )
    );
    ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <div class=”qbt-result” id=’qbt-results’> …

    $reg_no is the variable i want to set the current username to.
    the key, _qbt_register_no is a variable in custom post(qbt_calls) which is also the same
    as the username of the current user.
    Right now on the custom post page, the users enter their username (assigned as $reg_no), hit enter and the query runs.
    Rather than the users entering in their username manually in the custom post page and processing the query,
    I want to call it automatically when they are logged in and process the custom post page result for the current user.
    So the problem is setting the $reg_no to the current logged in username. thanks for your reply.

    did you try in this way :

    function qbt_query_result_from_post() {
    $user_info = wp_get_current_user();
    $reg_no = $user_info->display_name;
    if(!empty($reg_no))
    query_posts( array(
    'post_type' => 'qbt_calls',
    'meta_query' => array(
    array(
    'key' => '_qbt_register_no',
    'value' => $reg_no,
    )),
    'posts_per_page' => 1
    )
    );

    thanks thanks thanks zota, it worked.

    with pleasure, please close this topic

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘How to show current logged in username and ID’ is closed to new replies.