• Hello,

    I m new to php and am trying to get the current users ID from an external site as i want to use this ID in another table/site i have created, this is what i have done.

    i created a page called WP_USER_LOG.php

    and with this code:

    <?php
        require_once(www.mysite.co.uk/wp-includes/pluggable.php');
        $current_user = wp_get_current_user();
        echo 'User ID: ' . $current_user->ID . '';
    ?>

    then when i try and run the script from this page externally i would expect to get the id but i get the following:

    Server Error
    500 – Internal server error
    There is a problem with the resource you are looking for, and it cannot be displayed.

    hope someone can help me with this im getting desperate now

    many thanks

    iso

Viewing 10 replies - 1 through 10 (of 10 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    Hi iso96

    Are you missing a single quote at the beginnig of the string here?

    require_once(www.mysite.co.uk/wp-includes/pluggable.php');

    require_once expects a path on the server and not a URL. So for example you can’t pass https://www.mysite.co.uk/wp-includes/pluggable.php but you can do /var/www/wp-includes/pluggable.php. By default most web servers don’t allow you to include files from a remote addresss for security reasons.

    https://stackoverflow.com/questions/2752783/including-php-file-from-another-server-with-php

    Can you explain a bit more what it is you want to do?

    Maybe the Rest API does what you want.
    https://www.ads-software.com/plugins/json-rest-api/

    Thread Starter iso96

    (@iso96)

    Hi

    Thanks for helping me with this, i did that the quote missing but when i added it, it was still the same.

    tried this:

    <?php
    require_once(ABSPATH. ‘wp-includes/pluggable.php’);
    $current_user = wp_get_current_user();
    echo ‘User ID: ‘ . $current_user->ID . ‘
    ‘;
    ?>

    but also still not working.

    This is what i am try to do:

    I have an external site that shares the wordpress database.
    I want when someone logs into wordpress site they can do all the stuff on the wordpress site forum/blog etc. but i also have a menu with links to pages of my external site.
    So when the user clicks on a link to open a page eg ‘external site scheduler’ the page opens and recognises the userid whos logged into wordpress and then looks up that userid in my tables and shows that users ‘schedule’

    hope that makes sense.

    iso

    Moderator keesiemeijer

    (@keesiemeijer)

    Are both sites on the same server?
    If so, see this:
    https://codex.www.ads-software.com/Integrating_WordPress_with_Your_Website

    Thread Starter iso96

    (@iso96)

    Hi i have put uploaded the external site to the same server and its seems to be getting closer:

    <?php
    require_once(‘C:/Inetpub/vhosts/site.co.uk/test.site.co.uk/wp-includes/user.php’);

    $user_id = get_current_user_id();
    if ($user_id == 0) {
    echo ‘You are currently not logged in.’;
    } else {
    echo ‘You are logged in as user ‘.$user_id;
    }
    ?>

    the only problem is its ssays im not logged in when i am! at least its not showing an error message.

    any more ideas?

    Moderator keesiemeijer

    (@keesiemeijer)

    Have you tried this
    https://codex.www.ads-software.com/Integrating_WordPress_with_Your_Website#Grab_the_header

    Remove the require_once you have now

    And add this at the top of the page:

    <?php
    /* Short and sweet */
    define('WP_USE_THEMES', false);
    require('C:/Inetpub/vhosts/site.co.uk/test.site.co.uk/wp-blog-header.php');
    ?>

    Thread Starter iso96

    (@iso96)

    Hi,

    I can get their example code to work (the blog) but still cant get it to show the user ID!

    iso

    Moderator keesiemeijer

    (@keesiemeijer)

    It seems to work on my test install.

    Is the code you have currently this?

    <?php
    /* Short and sweet */
    define('WP_USE_THEMES', false);
    require('C:/Inetpub/vhosts/site.co.uk/test.site.co.uk/wp-blog-header.php');
    ?>
    
    <?php
    $user_id = get_current_user_id();
    
    if ($user_id == 0) {
    echo 'You are currently not logged in.';
    } else {
    echo 'You are logged in as user '.$user_id;
    }
    ?>

    Thread Starter iso96

    (@iso96)

    hello kept it as simple as possible and it still just returns 0 even though i am logged in?

    <?php
    define(‘WP_USE_THEMES’, true);
    require(‘C:/Inetpub/vhosts/site.co.uk/test.site.co.uk/Forum/wp-includes/user.php’);
    ?>
    <?php $user_ID = get_current_user_id();
    echo $user_ID;
    ?>

    This is the process

    open wordpress site and login
    open external site which runs the script above
    returns 0

    am i missing something?

    thanks

    iso

    Thread Starter iso96

    (@iso96)

    i ve cracked it, thank god for that.

    I use an IDE called Webdev and was using a the command PHPExecute, i tried ScriptDisplay and it worked.

    Thank you for all your help on this its very much appreciated.

    iso

    Moderator keesiemeijer

    (@keesiemeijer)

    You’re welcome. I’m glad you’ve got it resolved.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘get_current_user_id()’ is closed to new replies.