• Resolved wpostest

    (@wpostest)


    Hi Bro,
    Could you please help me, I would like to create a referral program on my website. I currently have 3 different registration forms. Each of them creates a different user account with a different role. I use the Ultimate Member plugin for this. Therefore, I would like to implement a field in each of these forms where the user can enter the referral code. I’m totally green and don’t know how to go about it.

    I would like to add that I added a text field to the forms where the user could enter the reference code. Then I entered the plugin editor and in template/register.php I wanted to create an appropriate function to collect data(refferal code) from the form and use your Action Hook -> wp_referral_code_after_refer_submitted. Only, I don’t know how I can collect the appropriate arguments for this function from here, i.e. new user id (new_user_id), referrer user id (referrer_user_id) and new reference code (new_user_refer_code)
    I don’t know if this is the right path, but maybe you can guide me.

    My code:

    				<?php 
    				/*
    					if(isset($_POST['refferal-4988'])){
    						do_action( 'wp_referral_code_after_refer_submitted', $new_user_id, $referrer_user_id, $ref_code, $new_user_ref_code );
    					}
    
    				function my_after_form($args){
    					$refferal = $_POST['refferal-4988'];
    					do_action( 'wp_referral_code_after_refer_submitted', $new_user_id, $referrer_user_id, $refferal);
    				}
    				*/
    				
    				add_action('user_register', function ($new_user_id) {
    					$user = WP_user::get_data_by('id', $new_user_id);
    					$new_user_ref_code = new WP_Refer_Code($new_user_id);
    					$refferal = $_POST['refferal-4988'];
    					do_action( 'wp_referral_code_after_refer_submitted', $new_user_id, $referrer_user_id, $refferal, $new_user_ref_code);
    					
    				}
    				?>

    All register.php code:

    <?php if ( ! defined( 'ABSPATH' ) ) exit; ?>
    
    <div class="um <?php echo esc_attr( $this->get_class( $mode ) ); ?> um-<?php echo esc_attr( $form_id ); ?>">
    
    	<div class="um-form" data-mode="<?php echo esc_attr( $mode ) ?>">
    
    		<form method="post" action="">
    
    			<?php
    			/**
    			 * UM hook
    			 *
    			 * @type action
    			 * @title um_before_form
    			 * @description Some actions before register form
    			 * @input_vars
    			 * [{"var":"$args","type":"array","desc":"Register form shortcode arguments"}]
    			 * @change_log
    			 * ["Since: 2.0"]
    			 * @usage add_action( 'um_before_form', 'function_name', 10, 1 );
    			 * @example
    			 * <?php
    			 * add_action( 'um_before_form', 'my_before_form', 10, 1 );
    			 * function my_before_form( $args ) {
    			 *     // your code here
    			 * }
    			 * ?>
    			 */
    			do_action( "um_before_form", $args );
    
    			/**
    			 * UM hook
    			 *
    			 * @type action
    			 * @title um_before_{$mode}_fields
    			 * @description Some actions before register form fields
    			 * @input_vars
    			 * [{"var":"$args","type":"array","desc":"Register form shortcode arguments"}]
    			 * @change_log
    			 * ["Since: 2.0"]
    			 * @usage add_action( 'um_before_{$mode}_fields', 'function_name', 10, 1 );
    			 * @example
    			 * <?php
    			 * add_action( 'um_before_{$mode}_fields', 'my_before_fields', 10, 1 );
    			 * function my_before_form( $args ) {
    			 *     // your code here
    			 * }
    			 * ?>
    			 */
    			do_action( "um_before_{$mode}_fields", $args );
    
    			/**
    			 * UM hook
    			 *
    			 * @type action
    			 * @title um_before_{$mode}_fields
    			 * @description Some actions before register form fields
    			 * @input_vars
    			 * [{"var":"$args","type":"array","desc":"Register form shortcode arguments"}]
    			 * @change_log
    			 * ["Since: 2.0"]
    			 * @usage add_action( 'um_before_{$mode}_fields', 'function_name', 10, 1 );
    			 * @example
    			 * <?php
    			 * add_action( 'um_before_{$mode}_fields', 'my_before_fields', 10, 1 );
    			 * function my_before_form( $args ) {
    			 *     // your code here
    			 * }
    			 * ?>
    			 */
    			do_action( "um_main_{$mode}_fields", $args );
    
    			/**
    			 * UM hook
    			 *
    			 * @type action
    			 * @title um_after_form_fields
    			 * @description Some actions after register form fields
    			 * @input_vars
    			 * [{"var":"$args","type":"array","desc":"Register form shortcode arguments"}]
    			 * @change_log
    			 * ["Since: 2.0"]
    			 * @usage add_action( 'um_after_form_fields', 'function_name', 10, 1 );
    			 * @example
    			 * <?php
    			 * add_action( 'um_after_form_fields', 'my_after_form_fields', 10, 1 );
    			 * function my_after_form_fields( $args ) {
    			 *     // your code here
    			 * }
    			 * ?>
    			 */
    			do_action( 'um_after_form_fields', $args );
    
    			/**
    			 * UM hook
    			 *
    			 * @type action
    			 * @title um_after_{$mode}_fields
    			 * @description Some actions after register form fields
    			 * @input_vars
    			 * [{"var":"$args","type":"array","desc":"Register form shortcode arguments"}]
    			 * @change_log
    			 * ["Since: 2.0"]
    			 * @usage add_action( 'um_after_{$mode}_fields', 'function_name', 10, 1 );
    			 * @example
    			 * <?php
    			 * add_action( 'um_after_{$mode}_fields', 'my_after_form_fields', 10, 1 );
    			 * function my_after_form_fields( $args ) {
    			 *     // your code here
    			 * }
    			 * ?>
    			 */
    			do_action( "um_after_{$mode}_fields", $args );
    
    			/**
    			 * UM hook
    			 *
    			 * @type action
    			 * @title um_after_form
    			 * @description Some actions after register form fields
    			 * @input_vars
    			 * [{"var":"$args","type":"array","desc":"Register form shortcode arguments"}]
    			 * @change_log
    			 * ["Since: 2.0"]
    			 * @usage add_action( 'um_after_form', 'function_name', 10, 1 );
    			 * @example
    			 * <?php
    			 * add_action( 'um_after_form', 'my_after_form', 10, 1 );
    			 * function my_after_form( $args ) {
    			 *     // your code here
    			 * }
    			 * ?>
    			 */
    			do_action( "um_after_form", $args ); ?>
    			
    			<div style="font-size: 3rem; color: #000;">
    				<?php 
    				/*
    					if(isset($_POST['refferal-4988'])){
    						do_action( 'wp_referral_code_after_refer_submitted', $new_user_id, $referrer_user_id, $ref_code, $new_user_ref_code );
    					}
    
    				function my_after_form($args){
    					$refferal = $_POST['refferal-4988'];
    					do_action( 'wp_referral_code_after_refer_submitted', $new_user_id, $referrer_user_id, $refferal);
    				}
    				*/
    				
    				add_action('user_register', function ($new_user_id) {
    					$user = WP_user::get_data_by('id', $new_user_id);
    					$new_user_ref_code = new WP_Refer_Code($new_user_id);
    					$refferal = $_POST['refferal-4988'];
    					do_action( 'wp_referral_code_after_refer_submitted', $new_user_id, $referrer_user_id, $refferal, $new_user_ref_code);
    					
    				}
    				?>
    			</div>
    		
    		</form>
    
    	</div>
    
    </div>
Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter wpostest

    (@wpostest)

    I have one more questions, is there any way to catch an error if the user gives the wrong reference code?

    Plugin Author shalior

    (@shalior)

    Hey. i did a quick look to your code. main problem with your code is that you are using hooks wrong. see the ‘wp_referral_code_after_refer_submitted’ hook is something you should add a function to. do_action(‘hook_name’) does not do any thing except allowing other developers to add functionality.

    i have not used plugin you mentioned so i cant help you with that. but there should be a hook so that you can get your custom field value without POST variable. using POST seems like a bad idea.

    in regard to your second question there is a static function on WP_Refer_Code class which allows you to user_id by refer code (WP_Refer_Code::get_user_id_by_ref_code( $ref_code )) if the result is empty then the refer code is not valid:

    	$user_id = WP_Refer_Code::get_user_id_by_ref_code( $ref_code );
              if(empty($user_id)) {
                // $ref_code is not valid 
              }
    Thread Starter wpostest

    (@wpostest)

    Thank you alot for your fast answer. It helped me very much. I spent some time to did it. I tried to different methods and finally i made it. Propably with not the best method but it work.

    I would also like to ask you if I did the right thing to edit the code of this plugin directly? Or should I include the following functions elsewhere? It would be nice if you answered this question for me ??

    I used the plugin Hook like you said and got the value there from that custom field.

    Now it looks like this, it may be useful to someone:

    function um_post_registration_approved_hook( $user_id, $args ) {
    	um_fetch_user( $user_id );
    	UM()->user()->approve();
    
    	//Referal System
    	$new_user_id = $user_id;
    	$ref_code = "";
    				
    	$new_user_ref_code = new WP_Refer_Code($new_user_id);
    	//Sprawdzamy czy textbox z jakiegos formularza zawiera kod referencyjny 
    	if($args["refferal"] != ""){
    		$ref_code = $args["refferal"];
    	}
    	else if($args["referal"] != ""){
    		$ref_code = $args["referal"];
    	}
    	else if($args["referall"] != ""){
    		$ref_code = $args["referall"];
    	}
    
    	if(!empty($ref_code) || $ref_code != ""){
    		$referrer_user_id = $new_user_ref_code->get_user_id_by_ref_code($ref_code);
    
    		do_action('wp_referral_code_before_refer_submitted', $new_user_id, $referrer_user_id, $ref_code, $new_user_ref_code);
    						
    		update_user_meta( $new_user_id, 'wrc_referrer_id', $referrer_user_id);
    						
    		$users_referred_by_referrer = get_user_meta( $referrer_user_id, 'wrc_invited_users', true );
    		if ( empty( $users_referred_by_referrer ) ) {
    			update_user_meta( $referrer_user_id, 'wrc_invited_users', [ $new_user_id ] );
    		} else {
    			$users_referred_by_referrer[] = $new_user_id;
    			update_user_meta( $referrer_user_id, 'wrc_invited_users', $users_referred_by_referrer );
    		}
    						
    		do_action( 'wp_referral_code_after_refer_submitted', $new_user_id, $referrer_user_id, $ref_code, $new_user_ref_code );
    	}
    }
    add_action( 'um_post_registration_approved_hook', 'um_post_registration_approved_hook', 10, 2 );

    And here is catching the exception:

    function um_custom_validate_reffcode( $key, $array, $args ) {
    	if ( isset( $args[$key] )) {
    		$ref_code = $args[$key];
    		$user_id = WP_Refer_Code::get_user_id_by_ref_code( $ref_code );
    
            if(empty($user_id)) {
                UM()->form()->add_error( $key, __( 'Podany kod referencyjny jest nieprawid?owy.', 'ultimate-member' ) );
    		}
    	}
    }
    add_action( 'um_custom_field_validation_ReffCode_Valid', 'um_custom_validate_reffcode', 30, 3 );
    Plugin Author shalior

    (@shalior)

    you’re welcome. glad to see you got the job done.

    this is what happens if you change the code of plugin directly: you’re gonna lose all those codes if i update the plugin. wordpress will replace all files with new updated version. so that is not the best option. instead you should have your own plugin which uses hooks to extent functionality.

    there are ways around this problem but never use a hack when you don’t have to.

    good luck and consider giving the plugin a kind rating.

    Thread Starter wpostest

    (@wpostest)

    Then is there any place to extend functionality plugin without write my own plugin ?

    And of course you get 5 stars from me ??

    Plugin Author shalior

    (@shalior)

    yes there is. functions.php file of your child theme.
    thanks.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Referral by referral code’ is closed to new replies.