• Hi,

    In my plugin Top 10, below is the code:

    add_action('wp_head','tptn_add_viewed_count');
    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?

Viewing 9 replies - 1 through 9 (of 9 total)
  • 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]

    I am also having this problem.

    I’m in the same boat. Any ideas?

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

    <?php
    $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>';
    }
    ?>

    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?

    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();
             ......

    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?

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

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

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘wp_get_current_user not working in 2.9?’ is closed to new replies.