! I have some questions regarding the affiliates plugin.

Hi !

I have some questions regarding the affiliates plugin.
I'm creating a front-end page with some of the shortcode you gave there.

Here is my questions :
1. I have created a custom template for this page. I want this page to be accessible only for "Super admin" or for the custom role "Partner". All the other visitors have to be redirected to the home page.

2. In this page, I want to display the referral link of the current user, but not all the details that come with the [affiliateuserdetails] shortcode

3. When people log in, I redirect them with a piece of code (that @aristath gave me there). In fact, I would like to redirect people with the role "partner" to the new front-end affiliate page. How can I do that ?

Thank you very much !

  • Jack Kitterhing

    Hi there @Noomia,

    Hope you're well today and thanks for your question.

    1: You can redirect them using the following code here, which is based by role http://code.tutsplus.com/tutorials/redirect-users-to-custom-pages-by-role--wp-33505

    2: Is this custom page template? So we can execute php, and you just want it to show there link correct? Do you also want the box for the custom link?

    3: There is a specific filter for this here http://codex.wordpress.org/Plugin_API/Filter_Reference/login_redirect which will allow you to redirect on login based by role.

    Thank you!

    Kind Regards
    Jack.

  • Noomia

    Hi @Jack

    1. This works well, thank you !

    2. Yes it's a custom page template. I just want to show their link. What do you mean by "also want the box for the custom link" ?

    3. Here is what I've write :

    public function my_login_redirect( $redirect_to, $request, $user ) {
      	global $user;
    
       	// Is there a user to check?
      	if ( $user ) {
      		$user_id = $user->ID; // Get user's ID
      		$user_role_name = $user->roles[0]; // Get user's role name
      	}
    
        if( isset( $user_role_name ) && 'partner' === $user_role_name ) { // If current user's role is "partner"
          return (get_permalink(118)); // Redirect him to "affiliate-dashboard" page
        } else {
      	  if ( isset( $user_id ) ) {
        		// Get the blogs of this user
        		$user_blogs = get_blogs_of_user( $user_id );
    
        		// Get the URL of the blog
        		foreach ( $user_blogs as $user_blog ) {
        			if ( !isset( $userblogid ) ) {
        				$userblogid = $user_blog->userblog_id;
        				return ('http://'.$user_blog->domain.'/wp-admin');
        			}
        		}
          } else { // If user is not a partner and has no blog, return to the homepage.
        		return home_url();
        	}
        }
      }

    The first part of the code works well : If I log in as a "partner", I'm well redirected to the affiliate front-page page.

    But if I'm a site owner, this doesn't work... I would like to redirect them to the admin of their primary page, without having this step :
    https://space.zeo.net/g/2o5lj

    Thank you !

  • Hoang Ngo

    Hi @Noomia,

    I hope you are well today.

    foreach ( $user_blogs as $user_blog ) {
        			if ( !isset( $userblogid ) ) {
        				$userblogid = $user_blog->userblog_id;
        				return ('http://'.$user_blog->domain.'/wp-admin');
        			}
        		}

    With this code, in most case, the main blog will return, so every admin will redirect to mainblog/wp-admin, which will cause your issue. You will need to get the right blog, which current user is admin, and then redirect them
    Please try this

    $blogs = get_blogs_of_user($user_id );
    
    	$user_blog ='';
    	foreach ( $blogs as $blog ) {
    		switch_to_blog($blog->userblog_id);
    		//check this user role
    		if(user_can($user_id,'manage_options')){
    			//this is admin of this blog
    			$user_blog = $blog;
    			break;
    		}
    		restore_current_blog();
    	}
    
    	if(is_object($user_blog)){
    		return ( 'http://' . $user_blog->domain . '/wp-admin' );
    	}

    This will run a loon from blogs, and get the blog which current user is admin, and then return it.

    If you have any issues please don't hesitate to let us know so we can assist
    Best regards,
    Hoang

  • Noomia

    Hi again @Hoang Ngo,

    $blogs = get_blogs_of_user($user_id );
    
    	$user_blog ='';
    	foreach ( $blogs as $blog ) {
    		switch_to_blog($blog->userblog_id);
    		//check this user role
    		if(user_can($user_id,'manage_options')){
    			//this is admin of this blog
    			$user_blog = $blog;
    			break;
    		}
    		restore_current_blog();
    	}
    
    	if(is_object($user_blog)){
    		return ( 'http://' . $user_blog->domain . '/wp-admin' );
    	}

    This doesn't work. Let's say the user has multiple blogs. If he logs in http://mysecondaryblog.mysaas.com/ , he has to be redirected to the admin of "mysecondaryblog". If he logs in http://mypremiaryblog.mysaas.com/ , he has to be redirected to the admin of "myprimaryblog". If he logs in http://mysaas.com/ (the main site), he has to be redirected to the admin of "myprimaryblog". Do you understand ? Right now, wherever he logs in, he is redirected to "myprimaryblog".

    Thanks !

  • Hoang Ngo

    Hi @Noomia,

    Sorry I have lost before, I think your requirement is redirect user to their blog he is admin.

    But if I'm a site owner, this doesn't work... I would like to redirect them to the admin of their primary page, without having this step :
    https://space.zeo.net/g/2o5lj

    Just for clarify, so when user logged in a site, and if he is admin of that site, he will redirect to the siteurl/wp-admin, and if not, this process will be like normal ?

    About the #2, It will be quicker for me if I can see the issue in action and the code relevant.

    Please advise,
    Thanks!!!

    Best regards,
    Hoang

  • Noomia

    Hi !

    Here is the entire process :

    When a user logs in :

    1. If the user role is "partner", redirect him to http://domain.com/partners/
    2. If the user role is "super-admin", normal process
    3. If the user is the admin of the current site, redirect him to http://blog.domain.com/wp-admin/
    4. If the user is not the admin of the current site or if the current site is the main site, redirect him to his primary blog admin http://blog.domain.com/wp-admin/

    Is that clear enough ?

    I'll send you the credentials for the FTP.

    Thanks a lot !

  • Hoang Ngo

    Hi @Noomia,

    Thanks for your information.

    So please try this code and let see if it help

    function my_login_redirect( $redirect_to, $request, $user ) {
    	global $user;
    
    	// Is there a user to check?
    	if ( $user ) {
    		$user_id        = $user->ID; // Get user's ID
    		$user_role_name = $user->roles[0]; // Get user's role name
    	}
    
    	if ( isset( $user_role_name ) && 'partner' === $user_role_name ) { // If current user's role is "partner"
    		return ( get_permalink( 118 ) ); // Redirect him to "affiliate-dashboard" page
    	} else {
    		if ( isset( $user_id ) ) {
    			//check does this supper admin
    			if ( user_can( $user_id, 'manage_network' ) ) {
    				return $redirect_to;
    			} elseif(user_can($user_id,'manage_options')){
    				return get_site_url().'/wp-admin';
    			}else{
    				//check this current user to find his blog
    				$blogs = get_blogs_of_user($user_id );
    				$user_blog ='';
    				foreach ( $blogs as $blog ) {
    					switch_to_blog($blog->userblog_id);
    					//check this user role
    					if(user_can($user_id,'manage_options')){
    						//this is admin of this blog
    						$user_blog = $blog;
    						break;
    					}
    					restore_current_blog();
    				}
    
    				if(is_object($user_blog)){
    					return ( 'http://' . $user_blog->domain . '/wp-admin' );
    				}
    			}
    		} else { // If user is not a partner and has no blog, return to the homepage.
    			return home_url();
    		}
    	}
    
    	return $redirect_to;
    }

    In the meantime, I'm updating the #2 solution.

    Best regards,
    Hoang

  • Jack Kitterhing

    Hi there @Noomia,

    Hope you're well today and thanks for your patience here.

    For two, I believe you'll want to take a look at the following code which is what outputs the standard affiliate link, that's what you need correct?

    <?php
    
    				if(get_user_meta($user_ID, 'enable_affiliate', true) == 'yes') {
    
    					$reference = get_user_meta($user_ID, 'affiliate_reference', true);
    					//echo "reference[". $reference ."]<br />";
    
    					$referrer = get_user_meta($user_ID, 'affiliate_referrer', true);
    					//echo "referrer[". $referrer ."]<br />";
    
    					$refurl = "profile.php?page=affiliateearnings";
    
    					$validreferrer = get_user_meta($user_ID, 'affiliate_referrer_validated', true);
    					//echo "validreferrer[". $validreferrer ."]<br />";
    
    					?>
    					<p><?php _e('<h3>Affiliate Details</h3>', 'affiliate') ?></p>
    					<p><?php _e('In order for us to track your referrals, you should use the following URL to link to our site:', 'affiliate'); ?></p>
    					<p><?php echo sprintf(__('<strong>%s?ref=%s</strong>', 'affiliate'), $affiliatelinkurl, $reference ); ?></p>

    Thanks!

    Kind Regards
    Jack.