Recording sign-up referrals

Posted by kento on July 11, 2011 9:11 pm

Since there have been several requests for an example on how to record a referral when an affiliate’s visitor has registered for an account, I’ve written an example plugin Affiliates Members which does just that.

1. Back-up your site and database.

2. Install and active the Affiliates plugin if you haven’t already.

3. Download the Affiliates Members example plugin, decompress the content and place it (a single file called affiliates-members.php) in your wp-content/plugins folder and then activate the plugin.

Now whenever a new user registers on your site, a referral will be recorded showing the new user’s name.

It’s a basic example on how to track sign-up referrals, so don’t expect more of it than just recording the new user’s name and other default data but this may actually be just enough for quite a few … comments, questions and intense cheers are, of course, welcome.

For those interested in how this works without wanting to bother with installing the example plugin, this is how it’s done:

add_action( 'user_register', 'affiliates_members_user_register' );
function affiliates_members_user_register( $user_id ) {
    if ( function_exists( 'affiliates_suggest_referral' ) ) {
        $userdata = get_userdata( $user_id );
        $data = array(
            'user-name' => array(
                'title'  => 'Username',
                'domain' => 'affiliates-members',
                'value'  => $userdata->display_name,
            )
        );
        affiliates_suggest_referral( get_the_ID(), __( 'New member', 'affiliates-members' ), $data );
    }
}
Posted in Plugins, WordPress
About kento
Computer Science Engineer, IT & Business Consultant, Drupal, WordPress, Scenario Planning ...

64 comments

  • Nice!! Seems to work for me!! So for a completely unrelated question…..are there any short codes to add affiliate data (clicks; referrals) to a page/post?

    • Shortcodes for that are not yet available, because up to now the intent was to provide information on the back end. If there is enough interest for an API (including shortcodes) that allows to pull off data for pages and visualize it there, then this can be included in a future release.

      • I definitely think the ability to track how much the affiliates have made and a front-end for their campaigns would be logical (and really cool!) If anyone wants to do that now, you just have to use the dashboard and some permission tricks :) works like a charm.

      • Patricia Reszetylo

        ABSOLUTELY!!! Affiliates need to know they can personally go to their stats. This (and adding the affiliates members to the plugin), and something that allows people to automatically get set up (or to sign up on their own) would be greatly appreciated.

        • Thanks for your comment Patricia, some of these features are scheduled to be implemented quite soon. On top of the list is for affiliates to be able to sign up directly.

  • JL

    I agree with the comments above. I desperately need to be able to do that with this plugin. I love how easy it is to add affiliates.

  • JL

    What Bryan & Patricia said above is exactly what I’m looking for Kento. I need my affiliates to be able to see their stats and commissions (I need to see this data too)

  • Hello, I wondering if you have any special pricing for the affiliate pro version for non profits. We are Motivate 2 Learn a registered 501 (C3)and we provide learning tools, motivation and more to schools (most under privileged). We want to start an affiliate program for fund raising purposes.
    Thank you

  • I just post a new job at freelancer.com , http://www.freelancer.com/projects/PHP-Javascript/Wordpress-referral-downline-sales-cart.html

    is there have any relation for this plugin…

  • Awesome plugin. Everything including this small code works Great. But I want to give credit when a contact form is filled. Currently I am using Custom Contact Forms by Taylor Lovet, and looking for a function to add to this code. Example
    add_action( ‘ccf_add’, ‘affiliates_members_user_register’ );
    function affiliates_members_user_register( $user_id ) {
    if ( function_exists( ‘ccf_add’, ‘affiliates_suggest_referral’ ) ) {
    $userdata = get_userdata( $user_id );
    $data = array(
    ‘user-name’ => array(
    ‘title’ => ‘Username’,
    ‘domain’ => ‘affiliates-members’,
    ‘value’ => $userdata->display_name,

    So Affiliate gets credit for “lead” and not for new user. But its not working as I want, Any suggestions would be great.

    • I haven’t worked with that plugin but there are two things that you should check/modify:
      - Does this ccf_add action really pass the $user_id?
      - Remove this: “if ( function_exists( ‘ccf_add’, ‘affiliates_suggest_referral’ ) ) {” this will certainly evaluate to false every time.
      Also there will be an integration with Contact Form 7 if you’re interested in using that plugin as an alternative.

  • Thanks for the plugin. I have not used it yet but understand the features and from what I have read so far its going to be just want I needed

  • When a new affiliate signs up on a website say on http://www.x.com, do they get a link something like this http://www.x.com/hhfyhss that they can send to their friends on social media and when their friends click on the link it tracks back to the registered affiliate?

  • I am trying to use the API as mentioned above but I am getting following error message.

    Call to undefined function wp_get_current_user() in /hermes/waloraweb068/b592/moo.oprotechcom/careerbreeder/wp-content/plugins/affiliates/lib/core/wp-init.php on line 785

    WordPress version 3.4

    I am using following code

    function affiliates_members_user_register( $user_id ) {
    if ( function_exists( ‘affiliates_suggest_referral’ ) ) {
    //$userdata = get_userdata( $user_id );
    $data = array(
    ‘user-name’ => array(
    ‘title’ => ‘Username’,
    ‘domain’ => ‘affiliates-members’,
    ‘value’ => “test”,//$userdata->display_name,
    )
    );

    affiliates_suggest_referral(“2″,’New member’, $data );
    }
    }

    affiliates_members_user_register(2);

  • Ray Masa

    Lets begin with intense cheers, thanks for the plugin, looks good. Added your registration plugin as well. Works well.

    Is there a way to add the affiliate id to the new user (in the wp user table)? This way when someone becomes a member, I would know which affiliate sent that person.

    Thanks,

    Ray

    • Ahhh intense cheers sounds amazing :) About your question, I see one fairly easy way to do that, but you would have to write a plugin that adds it as user meta when the new user is added. Is that an option?

      • Ray Masa

        Wow, that was a quick reply. Love the support. Thanks.

        I think I got it to work. I found a plugin (http://blog.ashfame.com/2010/11/add-custom-field-registration-wordpress/) that kind of does what you were suggesting, essentially adding the data to the usermeta table (which is fine). I had to modify the plugin very slightly by setting a cookie in header and then retrieving that in both wp_login and the script from the above plugin.

        So far it works, needs to test more. My only concern is that I had to modify wp_login (to retrieve the cookie) and that’s not good as the modification will disappear in the next wp update.

        Any suggestion on how to get around that (I know that is not exactly your script).

        Thanks,

        Ray

        P.S. if you want to include the hack for this in your files, I would be happy to send that all to you.

        • Thanks for getting back with your results as well :)
          I strongly advise not to hack WordPress core. It will be a PITA to maintain anyway. Do you really need the cookie? You can get the referring affiliate’s ID like this:
          $affiliate_id = Affiliates_Service::get_referrer_id();
          If you can use that to retrieve the referrer’s affiliate ID in the modified version of that plugin once a user registers, I think you’re ok.
          A pastebin of the code might be helpful to others, feel free to post a link if you want :)

          • PS: You need to include class-affiliates-service.php before making the call.

          • Ray Masa

            Sorry about not replying sooner, it all works correctly on the test server, but not on the production server. I spent two days trying to figure out why. I think there is something in the production server that is causing the issue, nothing to do with this script.

            As discussed, here is what I have done so far, and it works fine on the test server. In case someone else is looking for similar solution:
            - Download and install
            o Affiliates plugin (http://wordpress.org/extend/plugins/affiliates/)
            o Affiliates Members (http://www.itthinx.com/wp-content/uploads/2011/07/affiliates-members.zip)
            o Custom Registration Fields plugin (http://blog.ashfame.com/2010/11/add-custom-field-registration-wordpress/)
            - Modify Custom Registration Fields to add whatever field you would want to for the affiliate
            o Note that you probably would need to have the affiliate field in the Custom Registration Field as hidden. If you have an affiliate id in the URL, the will work fine. However, if you do not have an affiliate id in the URL, your registration form won’t work (it will ask for an affiliate id). You would need to add a statement to check for an empty field to deal with that as well.
            On the test server, I used cookies to store the affiliate id, in which case, you would need to set cookies in the header.php for the theme you are using. Here is a simple example on how to set a cookie in header:

            But as you suggested Kento, using affiliate id as below would be better:
            $affiliate_id = Affiliates_Service::get_referrer_id();
            I got distracted with trying to figure out the issue with production server, so have not yet tested the above implementation.

            As noted earlier, I had also modified the wp-login file, which is not really a good thing, as pointed out by Kento. Plus, I didn’t really needed to modify that, not sure why I did that to begin with. No need to touch that file.

            Hope this helps. Back to tearing my hair out trying to figure out my production server.

          • Thanks for the update on your progress.
            What differences are there between the test and the production server? If you could state some details (including OS, MySQL and PHP versions) we might get a clue to what’s wrong on the production server. Also do make sure that the code base on both deployments is exactly the same.

        • Ray Masa

          Both test and production have the same version of wp install as well as php and mysql. The only difference is that the product version got lots more plugins, and I suspect one of these plugins is cause the issue.

          The way I set it up is that when a visitor first comes to the website,the url will have an affiliate id (domain.com?affiliate=2). On this page (via header.php), I set a cookie to store the affiliate id. That works. When a visitor goes to a different page, the url does not have the affiliate id, but that’s ok because the id is stored in the cookie. And that works on all the pages on the website, except on the register page (domain.com/wp-login.php?action=register). For some reason the cookie disappears on that page, but only on the production server (works fine on the test server).

          I suspect one of the other plugins might be causing this. But before I turn all the plugins off to see which one, I will try your solution (Affiliates_Service::get_referrer_id();) and see if that solves it.

          Ray

  • Oscar

    Really excellent plugin.
    Please try to improve it more.

    Kento, is it possible for a user to register in affiliate program directly?

    Is it possible to assign a specific affiliate link for everybody who register in our website?

    Take care,

    • Hey Oscar, glad to hear you like the plugin. It’s being improved continuously thanks to all who use it and support its further development.
      Yes both are possible, have a look at the documentation.

  • Ronnie

    Will the Affiliate sign up work with an existing Membership site?

    For example, let’s say I already have a WordPress user registration form setup with another plugin. Can I automatically have them be registered as an affiliate using your plugin?

  • I was wondering if there was a way to track referrals as webform submissions. I want to track submissions from individuals using unique affiliate links (which works great by the way), but I have not been able to get a form submission to track as a referral. Is this possible as it is currently set up? Thanks.

  • Vladislav

    This plugin sounds great, and records referrals. But I need to see the exact user who has registered from a certain affiliate link (has been referred). It seems the plugin gathers this data, but I cannot seem to find where I can view it.

  • James Galloway

    Hey Kento. Awesome plugin :)

    I am trying to do something similar. If I quickly explain you may be able to help.

    First off, I am only using the free version at the mo, will I need to upgrade with this following scenario.

    I have a website which runs a members plugin(which is perfect), I have included your affiliate plugin now(which works perfect with the member plugin). What I want to achieve is people join my site for free through a members referral link. Inside the back office I want to convert them to join another external money making platform(through the external platforms referral link which belongs to that of the sponsor), when joined they return to the back office with me and enter their username from the external platform which places it into the database which can then be pulled when they refer… this repeats!

    Does that make sense Kento, I do hope so.

    I am no expert here but believe I am very close to achieving what I’m after. With your help I think I can pull this off.

    Much appreciated in advance.

    James :)

    • If I understand what you’re trying to do, you want referrals to be recorded when sales are made through an external platform. This would require an integration using the external platform’s API. That can involve quite an amount of consulting and development, depending no how that platform works, information that should be stored, payouts, …

      • James Galloway

        Hello Kento, thank you for getting back to me.

        No, what I am after is something to pull the sponsors username from the database to display in the new referrals back office and/or finsih of a referral link with it.

        We are not trying to detect anything from the external platform as when somebody joins it, they simply return and place it in their back office.

        I have created new fields one being ‘en_username’ and the other ‘en_sponsor’ user. I am able to pull data from the database with this code [s2Get user_field="en_username" /] and I am also able to add that to the end of their affiliate URL. However so far it is obviously displaying their own. I need it to pull that of their sponsor.

        So, It actually a simple system which is not going to be fully auto as I don’t intend to be communicating with the external comapnies data.

        So to hopefully explain it better. I send you my referral link to sign up free to my website, when inside there I convert you into joining an external platform and returning with the user id you created their to enter into your profile at my site. I obviously done this before you, so the data which is being called into your back office to create the link which you use to access the external platform is the user id i put into my profile(back office) before you. So this field [en_username] contains my username.

        I simply want to pull any data I choose(username) from a sponsors field to display in their referrals back office.

        please let me know if that makes any sense Kento lol :)

        • James Galloway

          Just so its clear. I dont want to pull my websites user ID. I want it to pull the user ID from the field I created [en_username]. So it is pulling the username of the external platform which they manually entered when they returned to my site.

          All your help much appreciated :)

          • Ok that sounds a lot clearer now :)

            As long as you have a means of retrieving the value of the field, you can pass it along with the $data array – following the example on this page:

            $sponsor = ...; // retrieve the value of the sponsor's name
            $data = array(
            'user-name' => array( 'title' => 'Username', 'domain' => 'affiliates-members', 'value' => $userdata->display_name ),
            'sponsor' => array( 'title' => 'Sponsor', 'domain' => 'affiliates-members', 'value' => $sponsor )
            );

  • James Galloway

    Hi Kento, when you get chance, could you check out the message above. I do think you can help me on this, Many thanks.

    Have a great day buddy,

    James :)

    • James Galloway

      thanks for the above answer Kento. Still not fully understanding though, sorry bud :)

      I have just upgraded to your pro version as I like the look of the extras which come with that. I have everything working nicely together now and am just outstanding this one thing.

      I know you are probably a busy guy but if you get chance buddy would you be able to send me further info or even a step by step on your reply to my email. Its frustrating being so close lol.

      Thanks greatly buddy, much appreciated.

      • Let me try it out myself and post a complete solution, sorry it took a while to get to this but the past days have been fully packed ;)

        • James Galloway

          Thank you Kento. I believe you almost have what I’m after already :) super stuff buddy. Simply pulling any custom field data from that of an affiliates sponsor and displaying in the affiliates back office!

          I look forward to seeing more buddy.

          I know your busy so any time is good Kento. Have a great day :)

      • Hi James,
        I’ve finally got a chance to take a further look, I think you can use s2′s function get_user_field() to retrieve what you need. So for the sponsor you are using, you can modify the $data like this:

        $data = array(
        'user-name' => array( 'title' => 'Username', 'domain' => 'affiliates-members', 'value' => $userdata->display_name ),
        'en_sponsor' => array( 'title' => 'Sponsor', 'domain' => 'affiliates-members', 'value' => get_user_field( 'en_sponsor' )
        );

        • James galloway

          super thanks for your help Kento… I know I should learn a lot more in this area but just a last quick question for you please.

          So If I have a custom field called ‘en_username’ which an affiliate would manually enter and submit this value, there referrals screen could display the username they entered into this field on there page by using the get code?… [s2Get user_field="en_username" /] if I modify with the details above(your snippet of code)? One thing though… where am I modifying within my site?. Obviously at the moment that s2get shortcode pulls and displays the input username of the same affiliate user.

          I know I should really get my learning head on and teach myself more but am so close at solving this thanks to you…

          I also posted a question on your affiliate share plugin page. Many thanks Kento. Have a great day, :)

          • Hey James,

            I’ll try to explain using the example with en_sponsor from before. When a referral is recorded, that field’s value is recorded along with the referral and you can show its value in the stats by adding the data parameter:

            [affiliates_affiliate_stats type="stats-referrals" data="en_sponsor"/]

            There are more options to that, please refer to Detailed referral overview in the documentation.

            Another case is when you are using the s2Get short code, then it will display the value of that field for the current user. See the difference?

            Cheers

    • Just got to it :)
      Cheers James

  • James galloway

    Hi Kento, first of all, awesome addition to the affiliate plugin(added [referrer_user] shortcode)! love it. couple of questions. How long until you have this in the pro version? plus, it is almost what I’m after… are you able to do short code to pull custom fields of a sponsor. As you know I have created a custom field for an affiliate to enter a user, I want to pull this user(custom field) for the referrer.

    Anyway buddy, another cheer for you, your plugin rocks and just keeps getting better.

    I know your a busy chappy, but hope to hear from you soon. Have a great day!

    Chears, James :)

    • Many thanks James :) Yes it’s already in there as well.

      • 007oo7

        Much appreciate your patience and and help so far Kento. Thanks buddy :) been going through all the docs including shortcodes and getting familiar with how it all works and am hugely impressed the more I learn.

        Anyway, to follow up further on my query… So far your info has been fab and I realized how close I am now with your shortcode:

        [referrer_user]

        This shortcode can be used to render information about the referring affiliate’s user account. The shortcode takes two optional parameters:

        direct – defaults to “false”, when “true” it will render information about the Direct affiliate when it applies.
        display – determines what to display; defaults to “user_login”; allowed values are “user_login”, “user_nicename”, “user_email”, “user_url”, “display_name”.

        The above code lets me pull sponsor info for the affiliate which is fab and I will be using this. However, this is so close to what I need. I simply want to pull a custom field in the same way… I have tried such things as this [referrer_user display="user_en_username" /] with en_username being the custom field. This simply pulls the sponsers username though. It was worth a shot lol :)

        Point is, because we are so close with this now can I be cheeky and ask you favour? would you be up for doing an update to this …[referrer_user]… so that we can pull custom fields? something like this [referrer_user display="user_CUSTOM_FIELD" /]

        Or contact me and hopefully arange some personal help. I have scanned the web, your docs, s2members docs, solid now so am not being lazy, I just feel you have what I need and we are very close.

        Much appreciated and look forward to an update Kento…

        By the way, deffo having the affiliate share plugin, looks awesome bud, have a fab day…

        James :)

        • Hey James, that could be a nice feature, adding it on the todo list for the next release! :)

          • James galloway

            hey that sounds good to me Kento, thanks buddy… In the meantime I am trying to fashion my own verison…. wow! lol. My thoughts are this so far. Until your possible update I can actually use the website field but would need to be able to 1)remove the http://(by default) 2) remove it from the signup form but be able to edit it from a profile edit field. Does that make any sense?

            So I could pull the entry Im after with this [referrer_user display="user_url"/]

            The URL field is of no use to me and I don’t want it during registration but do you agree that by entering it in a profile update this would acheive the same thing im after?

            Dont forget, the affiliates will not be getting their hands on their referral links until after they update their profile field(user_url)….

            Can I do this Kento?

            remove the website/url field from registration and then make it appear editable in profile after the affiliate is in?

            It still fulfills my needs and its already in place, I just need to tweak these things.

            1)how do I remove website/url field from registration?
            2)how do I remove the default value(http://)?
            3)how do I add it to the profile update field as editable?

            Anyway, will leave this with you while I learn more on the affiliate share plugin, cheers again buddy :)

          • Hi James,

            As the fields on the form that is provided are fixed, the best way to do this is to derive a custom version of the registration form. You can do this by using the form provided in lib/core/class-affiliates-registration.php, for example rename the class to Affiliates_Registration_Customized, in init() change

            add_shortcode( 'affiliates_registration', array( 'Affiliates_Registration', 'add_shortcode' ) );

            to

            add_shortcode( 'affiliates_registration_customized', array( __CLASS__, 'add_shortcode' ) );

            so that you can use the [affiliates_registration_customized] shortcode to render your own version of it. You would just have to remove the fields you don’t want in the code and you’re set :)

            Another way to do it would be to use a form you provide yourself which then uses one of the methods defined in the original class to create an affiliate entry. Once you have your user ID, you can call

            Affiliates_Registration::store_affiliate( $user_id, $userdata );

            The $userdata array expects to have entries for first_name, last_name and email.

  • Does this track those who sign up as an affiliate, or those who register as a user through the wp program?

  • Jon

    Hey Kento!

    Brilliant plugin. Thank you for all your hard work :)

    A previous comment in this thread mentioned customizing the affiliate registration fields – This is what I am trying to do.

    I need to create 7 additional input fields. (company name, address, phone number, etc.) And I need them to be required fields.

    I’m working on trying to figure out your previously mentioned instructions right now.. but I am very much a beginner with PhP – so it’s proving difficult to understand.

    > Can I just add some code to affiliates-registration.php to obtain those extra fields? What exactly would that be?

    I know you must be very busy.. I would sincerely appreciate any help you could offer.

    Please feel free to contact me via email.

    Thank you again!

    • antonio

      Hi,
      the affiliate registration form is fixed and you can’t add any fields there unless you derive a customized version. The form is kept simple to make registration as easy as possible. Any additional information can be added to the user profile, see Useful plugins.

      cheers

Leave a comment

Your email address will not be published. Required fields are marked *

*


four × 7 =

Please follow the Support guidelines.

Premium Support is available for customers in the Forums.