wp_get_current_user not working in 2.9? (10 posts)

  1. Ajay
    Posted 5 years ago #


    In my plugin Top 10, below is the code:

    function tptn_add_viewed_count() {
    	global $post, $wpdb, $single;
    	$table_name = $wpdb->prefix . "top_ten";
    	$tptn_settings = tptn_read_options();
    	$current_user = wp_get_current_user();
    	$post_author = ( $current_user->ID == $post->post_author ? true : false );
    	if((is_single() || is_page())) {
    echo '<!-- Top 10 : '.$current_user->ID.' & '.$post->post_author.' -->';
    		 if (!(($post_author)&&(!$tptn_settings['track_authors'])))  {
    			$id = intval($post->ID);
    			$output = '<script type="text/javascript" src="'.get_bloginfo('wpurl').'/wp-content/plugins/top-10/top-10-addcount.js.php?top_ten_id='.$id.'"></script>';
    			echo $output;

    I have noticed that this doesn't work anymore, the $post->post_author and $current_user->ID both return blank values.

    This wasn't a problem before I upgraded to 2.9. Can anyone else confirm this?

  2. srdusky
    Posted 5 years ago #

    I have several installations, and the $current_user function seems to be broken on my 2.9 installation. Let me know if you figure this one out. [email moderated]

  3. Jobeesh
    Posted 5 years ago #

    I am also having this problem.

  4. beredon
    Posted 5 years ago #

    I'm in the same boat. Any ideas?

  5. ibr
    Posted 5 years ago #

    I made a workaround for this issue on my WP 3.0 install:

    $thisuser = wp_get_current_user();
    if ( 0 == $thisuser->ID ) {  // Not logged in.
    } else {  // Logged in.
    echo '<li><a href="' . get_option('siteurl') . '/wp-admin/profile.php">Logged in as ' . $thisuser->display_name .'</a></li>';
  6. torvad
    Posted 5 years ago #

    I'm having the same problem. Did anyone find a good solution to the problem?

    wp_get_current_user() returns false, and it's because $current_user is empty.

    When you log into WP, in which function or class is this variable instatiated with a user object?

  7. toyNN
    Posted 4 years ago #

    The wp_get_current_user() function appears to work fine for me (2.9.2 ->). Make sure its called after testing if the user is logged otherwise it will return NULL.

    @torvad - $current_user var is populated by the function call. If the function returns NULL then the var will be empty.

    As @ibr shows - you need to test if the user is logged in before making the function call. Here's a similar way to test,

    <?php global $user;
        if (is_user_logged_in()) {
            $user = wp_get_current_user();
  8. torvad
    Posted 4 years ago #

    is_user_logged_in() returns false inside the function &get_posts() which is in query.php

    When you edit a post in WP the file admin/post.php is called. I checked and found out that is_user_logged_in() returns true in this file.

    Why does it return false inside query.php? I am logged in...

    Is there a function that logs off the user when calling this file?

  9. 2nic
    Posted 4 years ago #

    for me the problem appeared when i activated, try to activate theme_test_drive plugin.
    it was solved when i deleted the plugin

  10. 2nic
    Posted 4 years ago #

    i had the same problem with theme-tester - had to delete it desactivate it.

Topic Closed

This topic has been closed to new replies.

About this Topic