Support » Plugins » get_current_user_id() returns 0 in external PHP page in plugin directory

  • Before this gets marked as a duplicate, please read, I have read several of the solutions and none of them work.

    I am writing a plugin that has an non WP page in it to record votes in a database table and return JSON of the vote counts.

    The offending code is:

    define('WP_USE_THEMES', false);
    require( $_SERVER['DOCUMENT_ROOT'].'/test/wp-blog-header.php' );
    do_action( 'login_init' );
    echo $user_id;

    this displays 0

    I tried it with:

    require( $_SERVER['DOCUMENT_ROOT'].'/test/wp-load.php' );
    echo $user_id;

    and get the same result – 0.

    I have a shortcode that uses get_current_user_id() in the same plugin folder and it works fine in a WP created page, but in my custom page do_shortcode( ‘[custom-shortcode]’ ); displays 0 for the $user_id.

    All other WP functions appear to be working – get_site_url() works and I have access to other php files in my plugin that use $wpdb and that all work…

    My WP install is in the test/ directory under my main site – hence the ‘/test/’ in the require statements. The PHP file in question is in the root of the plugin directory.

    There has got to be something simple I am missing – any ideas?

    Full page code – (obviously there is more output than just JSON – I’m trying to get the user issue figured out…):

      define('WP_USE_THEMES', false);
      require( $_SERVER['DOCUMENT_ROOT'].'/test/wp-blog-header.php' );  //tried this with wp-load.php too
      do_action( 'login_init' );
      $_SESSION['key'] = "jkshdfkjhdsfjkhaksdfkjsahfjkdbfbudsfbvjhbvjhbrukbuhbvjkhfbvjs";  //purposely set for testing
      $post_id = $_GET['id'];                                      //get post_id (SHOULD BE FROM QUERY STRING)
      echo "post_id:{$post_id}<br>";
      $url_session = $_GET['key'];                                 //get session key from URL
      echo "url session:{$url_session}<br>";
      $session_id = $_SESSION['key'];                              //get session id
      echo "session ID:{$session_id}<br>";
      $user_id = get_current_user_id();                            //get current logged in user
      echo "user_id:{$user_id}<br>";
      $vote = $_GET['vote'];                                       //get vote from query string
      echo "vote:{$vote}<br>";
      echo "site url:" . get_site_url() . "<br>";
      echo "referrer:". $_SERVER['HTTP_REFERER'] . "<br>";
      $error = 0;                                                  //set error count to 0
      $error = ( FALSE !== get_post_status( $post_id )                      ? $error : $error + 1 );  //check to see if post exists
      echo "error after post status check:{$error}<br>";
      $error = ( $user_id != 0                                              ? $error : $error + 1 );  //check to see if user logged in
      echo "error after userid check:{$error}<br>";
      $error = ( $url_session == $session_id                                ? $error : $error + 1 );  //check to see if session is valid
      echo "error after session check:{$error}<br>";
      $error = ( strpos("read_it,skip_it", $vote ) !== false                ? $error : $error + 1 );  //check to see if the vote is valid
      echo "error after vote type check:{$error}<br>";
      $error = ( FALSE !== strpos($_SERVER['HTTP_REFERER'], get_site_url()) ? $error : $error + 1 );  //check to url referrer
      echo "error after referrer check:{$error}<br>";
      if($error == 0){  //if all validity checks pass
        $votes = record_postvotes($user_id, $post_id, 'skip_it');   //submit the vote and get return values
        $output = "{\"votes\":[{\"ReadIt\":{$votes[0]['read_it']},\"SkipIt\":{$votes[0]['skip_it']}}]}";
      } else {
        $output = '';               //otherwise, return nothing
      } //end error check
      echo $output;

  • The topic ‘get_current_user_id() returns 0 in external PHP page in plugin directory’ is closed to new replies.