how can users be automatically affiliates

Hi, is there a tips or workaround that could avoid all users to edit their profiles to be affiliates ?

Can we set something that cause every users are affiliates ?

If not, as an admin, could we 'activate' a users as an affiliate ?

Tks!

  • Vinod Dalvi

    Hi @Sebastien,

    I hope you are well today and thank you for your questions.

    We can achieve this by developing custom code if you are comfortable with custom coding.

    We can just remove the Affiliate page for all users where users became affiliate on the following path.

    Admin Area -> Profile -> Affiliate Referrals.

    Then make every users affiliate using code.

    Please advise.

    Kind Regards,
    Vinod Dalvi

  • Vinod Dalvi

    Hi @Sebastien,

    Glad to hear you are comfortable with custom coding.

    Add the following code in the functions.php file of your child theme to avoid all users except administrator of site to edit their profiles to be affiliates by removing affiliate page for all users where users became affiliate on the following path.

    Admin Area -> Profile -> Affiliate Referrals.

    add_action( 'admin_menu', 'remove_affiliate_page', 999 );
    add_action( 'network_admin_menu', 'remove_affiliate_page', 999 );
    function remove_affiliate_page() {
    	if ( ! current_user_can( 'manage_options' ) ) {
    		  remove_submenu_page( 'users.php', 'affiliateearnings' );
    		  remove_submenu_page( 'profile.php', 'affiliateearnings' );
    	}
    }
    
    $blogusers = get_users();
    // Array of WP_User objects.
    foreach ( $blogusers as $user ) {
    	if ( ! user_can( $user->ID, 'manage_options' ) ) {
    		update_user_meta( $user->ID, 'enable_affiliate' , 'yes');
    	}
    }

    The above code will remove whole Affiliate Referral page from users which contains "Enable Affiliate links" and "PayPal Email Address" fields ( see the attached screenshot ) and will enable affiliate for all users except administrators.

    We have enabled Affiliate links filed to yes for all users developing custom code but what about "PayPal Email Address" field?

    Should i use user profile email address as "PayPal Email Address" or add another field as "PayPal Email Address" in the user profile?

    Please advise.

    Regards,
    Vinod Dalvi

  • Sebastien

    Hi Vinod, thanks for the code. I tried it and it is running as expected. Every new users is listed in affiliate.

    Now of course it would be usefull if links would be enabled. I tried the display stats shortcode on a user page and the stats doesn't display as the user has not enable the url link and his email for paypal.

    So, i suggest to add another field as paypal adress in the userprofile and enable by default url affiliate links.

    thkx.

  • Jack Kitterhing

    Hi there @Sebastien,

    Hope you're well today and thanks for your question! :slight_smile:

    By default the affiliate links are disabled, but you can output any of the affiliate setup using the shortcodes so they can enable them.

    You can use these shortcodes.

    [affiliatelogincheck] – place this at the top of the page that you are planning on using the rest of the shortcodes on and it will display a login message if the user isn’t logged in.
        [affiliateuserdetails] – mirrors the affiliate details form from the admin area (yellow box)
        [affiliatestatstable] – mirrors the large click statistics table
        [affiliatevisitstable] – mirrors the referrers this month table
        [affiliatetopvisitstable] – mirrors the top referrers table
        [affiliatestatschart] – mirrors the main statistics charts
        [affiliatevisitschart] – mirrors the referrers charts
        [affiliatebanners] – displays the banners you add to your affiliate settings

    Would that work for you here? :slight_smile:

    Thanks!

    Kind Regards
    Jack.

  • Sebastien

    Hi Jack, thanks for your answer. But with respect i dont think this is the way.
    If you look at the thread before your answer, i was asking if it was possible to 'enable' automatically an affiliate.

    The provided code works ! so every users in the backend are affiliate by default….but..: Even if a user is an affiliate, the profile page with a shortcode (for example [affiliateuserdetails]) doesnt work because the affiliate doesnt have an url and a email paypal adress. He has first to create a link and submit an address.

    It's why Vinod ask me something about the email, in order i guess to create automatically the url and email on the account, without any user intervention. That would be perfect and i hope to have an answer from Vinod soon :wink:

    thks and have a nice week end.

  • Vinod Dalvi

    Hi @Sebastien,

    So, i suggest to add another field as paypal adress in the userprofile and enable by default url affiliate links.

    To achieve this try using the following code in the functions.php file of your child theme overwriting previously provided code.

    add_action( 'admin_menu', 'remove_affiliate_page', 999 );
    add_action( 'network_admin_menu', 'remove_affiliate_page', 999 );
    function remove_affiliate_page() {
    	if ( ! current_user_can( 'manage_options' ) ) {
    		  remove_submenu_page( 'users.php', 'affiliateearnings' );
    		  remove_submenu_page( 'profile.php', 'affiliateearnings' );
    	}
    }
    
    $blogusers = get_users();
    // Array of WP_User objects.
    foreach ( $blogusers as $user ) {
    	if ( ! user_can( $user->ID, 'manage_options' ) ) {
    		update_user_meta( $user->ID, 'enable_affiliate' , 'yes');
    		$reference = aff_build_reference( $user );
    		update_user_meta($user->ID, 'affiliate_reference', $reference);
    	}
    }
    
    add_action( 'show_user_profile', 'add_affiliate_paypal_field' );
    add_action( 'edit_user_profile', 'add_affiliate_paypal_field' );
    
    function add_affiliate_paypal_field( $user ){
        ?>
            <h3>Custom Affiliate Profile Field</h3>
    
            <table class="form-table">
                <tr>
                    <th><label for="facebook_profile">Affiliate PayPal Email Address</label></th>
                    <td><input type="text" name="affiliate_paypal" value="<?php echo esc_attr(get_the_author_meta( 'affiliate_paypal', $user->ID )); ?>" class="regular-text ltr" /></td>
               </tr>
               <tr>
                    <th><label for="facebook_profile">Your Affiliate URL</label></th>
                    <td><?php echo is_ssl() ? 'https' : 'http'; ?>:<input type="text" name="affiliate_referrer" value="<?php echo esc_attr(get_the_author_meta( 'affiliate_referrer', $user->ID )); ?>" class="regular-text ltr" /></td>
               </tr>
            </table>
        <?php
    }
    
    add_action( 'personal_options_update', 'save_affiliate_paypal_field' );
    add_action( 'edit_user_profile_update', 'save_affiliate_paypal_field' );
    
    function save_affiliate_paypal_field( $user_id ){
        update_user_meta( $user_id,'affiliate_paypal', sanitize_text_field( $_POST['affiliate_paypal'] ) );
        update_user_meta( $user_id,'affiliate_referrer', sanitize_text_field( $_POST['affiliate_referrer'] ) );
    }

    Best Regards,
    Vinod Dalvi

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.