• Resolved 00coday

    (@00coday)


    get_current_user_id() returns 0 in external PHP page

    Sorry… I posted the original in the plugins & hacks group…

    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. I have spent the last few hours googling and can’t seem to figure out how to get get_current_user_id() to return anything but 0 in an external PHP file

    The offending code is:

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

    this displays 0

    I tried it with:

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

    and get the same result – 0.

    I have a shortcode that works fine in a WP created page, but in my custom page do_shortcode( ‘[custom-shortcode]’ ); displays 0 as 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 they 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…):

    <?php
    /*
      vote.php
    */
    
      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;
    
    ?>

Viewing 1 replies (of 1 total)
  • Thread Starter 00coday

    (@00coday)

    So I figured it out… The problem I was the URL I was using to access the non WordPress PHP file. The return of get_site_url() was
    https://www.mysite.com
    The URL I was using to access the non WP PHP file was
    https://mysite.com/test/wp-content/plugins/plugindir/vote.php?id=2076&vote=read_it&key=jkshdfkjhdsfjkhaksdfkjsahfjkdbfbudsfbvjhbvjhbrukbuhbvjkhfbvjs
    The difference was the “www” in the vote page address. When I added it in and accessed
    https://www.mysite.com/test/wp-content/...
    all of the user functions started working.

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