$ad_code = str_replace("xxxx",$_SESSION['user_id'],$resultset->description);
    $ad_code = str_replace("XXXX",$_SESSION['user_id'],$ad_code);
    echo '<tr>';
    echo '<td class="col2"><strong>'.$resultset->name.'</strong></td>';
    echo '<td><textarea cols=65 rows=5>';
    echo $ad_code;
    echo "</textarea></td>";                
    echo '</tr>';

I'm using a plugin called WordPress Affiliate Platform, which created (I think) users in a seperate table than the typical WP users so as not to mix affiliate users and WP users.

I'd like to retrieve the user ID of the logged in affilite so I can list the affiliate's current page referral url:

  • Example of a site wide referral url: http://domain.com/?ap_id=AMEEKER
  • Example of a page specific referral URL: http://domain.com/myproduct?ap_id=AMEEKER

The plugin itself doesn't offer that option, instead just giving the instructions to tell affiliates to paste ?ap_id=AMEEKER after the current page URL to get their affiliate link. That's fine, but a little tedious and can open affiliates up to errors that aren't really their fault.

I'm sure there's a way to just echo out the current page URL, followed by ?ap_id= and the affiliate's user id, but I'm having trouble figuring out how to retrieve the affiliate's user ID.

The plugin itself does it by transforming xxxx to the user id somehow.


I've pasted above some code from the affiliate's dashboard, and area where the plugin DOES pull the affiliate's ID and display it dynamically for the logged in user. Looking at that template in the plugin's files, I found what I think is the code that's dynamically turning XXXX into the actual affiliate ID but I don't know what to do with this, or if this alone can help me get what I want. I mean, I can look at it and KIND of see what it's doing, but not enough to know if or what I can do with it now!

(not a programmer, more like a tinkerer).

I've asked the plugin author, who seems to give very good support, but this feature has been requested it seems on their site several times and their answer is the same. No hard feelings, but I'd like to see if it can be done.

Any help would be appreciated.

  • Based on that code I'd guess it's in the $ad_code var. Where/how do they build that? – curtismchale Jun 26 '12 at 20:19
  • I don't know. :-( Not trying to be silly, but I really don't know where to look or even what to look for. – angiemeeker Jun 26 '12 at 20:29

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
    // 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.


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.

  • I knew they were two diff. groups of users. I want to display the affiliate id if the affiliate is logged in, not paying attention to WP users. I'm displaying content elsewhere IF the affiliate is logged in using this: if(aff_main_check_security()) { add_action( 'genesis_before_content_sidebar_wrap', 'include_aff_info' ); function include_aff_info() { require(CHILD_DIR.'/aff_info.php'); }; } else { echo ""; } If the aff. logins, they stay logged in across the site. – angiemeeker Jun 26 '12 at 21:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.