Echo user id of users in seperate table

After taking a look at the code for the plugin, it doesn’t look like there’s an easy way to do this.

Affiliates are stored in one table that has no direct relationship to your WP users table – this means you can have users who aren’t affiliates and you can have affiliates who aren’t users. That’s why there’s a big “Import WP Users” option … it’s not automatic.

When an affiliate logs in, there’s code that automatically starts a PHP session, stores their affiliate ID as a session variable, and also sets it as a cookie so it can be retrieved later:

global $wpdb;
$affiliates_table_name = WP_AFF_AFFILIATES_TABLE;        
$result = $wpdb->get_row("SELECT * FROM $affiliates_table_name where refid='$userid'", OBJECT);
    
if($wp_hasher->CheckPassword($password, $result->pass))
{
    // this sets session and logs user in
    if(!isset($_SESSION)){@session_start();}
    // this sets variables in the session
    $_SESSION['user_id']= $userid;
    setcookie("user_id", $userid, time()+60*60*6, "/"); //set cookie for 6 hours

    // ... and so on
}

The plugin, at this point, isn’t using WordPress’ user management system at all.

Summary

The affiliate IDs you’re seeing are not the same thing as usernames in the system, and there’s no way you can run a query to pull a username/id from WordPress’ user table based on an affiliate ID (or vice-versa) because there is no data relating the two anywhere in the system.


Getting the Affiliate ID

If all you want to do is get the affiliate ID if the user is logged in, then all you need to do is look at either the cookie collection or current session:

function get_current_affiliate_id() {
    $affiliate_id = false;

    if ( isset( $_SESSION ) && isset( $_SESSION['user_id'] )
        $affiliate_id = $_SESSION['user_id'];

    if ( ! $affiliate_id && isset( $_COOKIE['user_id'] ) )
        $affiliate_id = $_COOKIE['user_id'];

    return $affiliate_id;
}

If the user is logged in, then both the session variable and the cookie should be set. This function will then return the user’s ID (the cookie check is just a fallback should something happen to your server-side sessions … i.e. a server reboot while someone is using the site). If the user is not logged in, the function will return false.

Once you have their ID, you can use other functions that ship with the plugin to get whatever info you need.