Support » Plugin: Football Pool » How to include pool choice in registration form?

  • Resolved PC1271

    (@pc1271)


    To make the login and register screens look better, I added Ultimate Member.
    Everything is working just fine, but I want to give the user the option to choose which pool they want to play in (free or paid).

    For that I probably need to add a filter or an action to my functions.php and hook it inside the Ultimate Member registration form somehow.
    But since I’m not very php-savvy, I need some assistance in this department.

    Is there anyone out here who already got this working or could you give me some pointers on how to get started, I would very much appreciate that.

    The page I need help with: [log in to see the link]

Viewing 12 replies - 1 through 12 (of 12 total)
  • Plugin Author AntoineH

    (@antoineh)

    Do you have the option to name the field “league”? If so, my plugin will simply pick up the value in the new registration hook and you don’t have to do anything special.

    If not, then you can copy the hook I have in my code, but then change the code of the Football_Pool::new_pool_user function to pick up the value in your field.

    add_action( 'user_register', array( 'Football_Pool', 'new_pool_user' ) );
    

    Hi Antoine,

    Thanks for replying. However, I’m afraid I don’t fully understand what you are suggesting.
    Anyway, since yesterday, I learned a few things: I used the ‘Advanced Custom Field’ plugin to create a custom field named “league”.
    Initially, I hoped by using the shortcode [acf field=”league”] the selector field would show up on the frontend of my registration field. Now I realize that this is not how acf works. Apparently, the custom field is only shown in the backend WordPress editor and the shortcode can be used to show the applied value on the frontend.

    My second attempt was to create a selector field with some html code. I called that field ‘league’. Now I have a selector field on the frontend, but is is not connected to the ‘pool’ field of the Football Pool plugin.

    Since I couldn’t get the ‘easy’ options above, working, the next approch would be to copy and change some php. What I understand from your suggestion is that I should find and copy the code for your function with the name new_pool_user and change whatever I need to change to get it working for me.
    Let’s say, I call that new function my_new_pool_user and then add it with
    add_action( 'user_register', array( 'Football_Pool', 'my_new_pool_user' ) );
    would this add a working selector field to my registration form (Ultimate Member)? Or should I first also find out the name of the hook I need to use instead of ‘user_register‘?

    Maybe, with my limited php understanding, I could get this to work, if only I could find your new_pool_user which I’m supposed to copy and change.

    Probably your first approach is easier if only I would know how to create that selector field called ‘league’ inside my registration form so that it could get picked up by your plugin. It should be possible to add custom fields via the Ultmate Member form editor, but I see no easy way to get this done. I found this link: https://gist.github.com/champsupertramp/c1f6d83406e9e0425e9e98aaa36fed7d, but this also involves tinkering with php.

    Could you elaborate some more about how to add this field which would make things easy?

    Best regards,
    Patrick

    • This reply was modified 4 months, 3 weeks ago by  PC1271.
    Plugin Author AntoineH

    (@antoineh)

    I have no experience with the UM plugin, but according to this tutorial it should be very easy to add an extra field to the form. No HTML or PHP knowledge required.

    Only thing to check, is if the outputted field has name="league" in the registration form. If yes, then you’re all done.

    If not, then you’ll have to fiddle around with my code and get the correct value upon registration. If you let me know, I can guide you in the correct direction.

    • This reply was modified 4 months, 3 weeks ago by  AntoineH.

    Hi Antoine,

    I could slap myself in the face for not understanding how easy it could be to add a custum field to UM.
    Anyway, now I managed to define a selector field with the name “league” and added that to the form. In the following screen capture, you can see how I defined that custom selector: https://ibb.co/dgfvBH

    I tested the registration form to see if the value of the field is picked up by your plugin, but I can’t get it done. No matter what value I give the field in the registration form, when I look at the user in the football pool, they’re always in the ‘for free’ pool.

    I even tried adding a blank to the options in the selector field to make 3 options:
    1. blank
    2. for free
    3. for money
    but that doesn’t seem to help. Every new registered user goes to the free pool (which is the default value of the custom option field in UM).

    Any other suggestions to make this work?

    Best regards,
    Patrick

    Plugin Author AntoineH

    (@antoineh)

    Hi Patrick,

    I installed the UM plugin to see how it works. The problem is caused by the way UM adds the options in the HTML. UM adds the league names as values in the HTML and my code expects integers (the league ID). So, the solution is to find a way to add the options as name-value pairs that result in something like below in the registration form (use the correct league ID’s from your install as values):

    <select name="league">
      <option></option>
      <option value="2">for money</option>
      <option value="3">for free</option>
    </select>
    

    Not sure if UM supports this. If not, maybe you can add a feature request with the plugin developers.

    Or you create some code as I suggested earlier. But then do a mapping/lookup of the string-value to the league ID before you insert it with the user. So, basically that would be the exact same code as is used in my plugin, but with a few lines of extra logic to get the correct league ID.

    Plugin Author AntoineH

    (@antoineh)

    Actually, I don’t think it is possible to stop my code from firing and that sets the league to the default. So your filter should fire later and only do an update (values are already inserted).

    I now know that $league field in the UM registration form gets a string value (‘for money’), because I can see this value in the new user’s profile page: http://wk2018.persawa.be/user-2/sus/

    Based on the example you provided, I installed an extension plugin in which I try to convert the string to a number and after the conversion, I try to update the database field for the new user.

    Here’s my code:

    <?php
    /**
     * Plugin Name: Football Pool New Users
     * Description: Replace UM pool text by number.
     * Version: 1.0
     * Author: My Name
     * Author URI: mailto:my_name@gmail.com
     * License: MIT
     */
     
    // Save this plugin in the wp-content/plugins folder and activate it //
     
    class FootballPoolSetLeagueOnRegistration {
        public static function no_fp_plugin_error() {
            echo '<div class="error"><p>The Football Pool plugin is not activated. Make sure you activate it so the Football Pool extension plugin has some use.</p></div>';
        }
         
        public static function init_extension() {
            // Display a message if the Football Pool plugin is not activated.
            if ( ! class_exists( 'Football_Pool' ) ) {
                add_action( 'admin_notices', array( __CLASS__, 'no_fp_plugin_error' ) );
                return;
            }
            // Users are automatically added, so after the save we just set the player in the chosen pool.
    		
            add_action( 'footballpool_new_user', array( __CLASS__, 'set_league' ), null );
        }
         
        public static function set_league( $id ) {
            global $wpdb;
            $prefix = FOOTBALLPOOL_DB_PREFIX;
             
            $pool = new Football_Pool_Pool();
            if ( $pool->has_leagues ) {
                
                echo $league;                                           // for debugging
                			
    			switch ($league) {
    				case " ":
    					// echo "league is blank";
    					$league = " ";
    					break;					
    				case "for free":
    					// echo "league = for free";
    					$league = 3;
    					break;
    				case "for money":
    					echo "league = for money";                      // for debugging
    					$league = 2;
    					break;
    			}
    			update_user_meta( $id, 'footballpool_league', $league );
    			
                $sql = $wpdb->prepare( "UPDATE {$prefix}league_users SET league_id=$league WHERE user_id = %d", $id );
                $wpdb->query( $sql );
            } else {
                $pool->update_league_for_user( $id, 0 );
            }
        }
    }
     
    add_filter( 'plugins_loaded', array( 'FootballPoolSetLeagueOnRegistration', 'init_extension' ) );

    Unfortunately, this codes doesn’t seem to have any result. That’s no surprise because of my limited php-savvy. I’m not even sure if this code is ever triggered and if it is, when.
    And even if it’s triggered, does it do what it’s supposed to do?

    When I refresh the UM profile page, it seems nothing happened to the $league field. It still says ‘for money’. If it had been changed by my code, it should give a number value, right?

    I start thinking this problem can only be solved by the developer of UM by changing the option field to a number instead of a string. Only then will both UM and your plugin speak the same language. Because, even when I manage to change the $league variable to the number expected by your plugin, it will make no more sense in the UM profile screen.

    Plugin Author AntoineH

    (@antoineh)

    Hi,

    I can have a look at your code tonight. Both plugins should be able to work together.

    And changing the string to a number value will only affect my plugin, if done right, not the UM one. So profile will look fine.

    But still, the name-value feature would be a great feature request for UM.

    Plugin Author AntoineH

    (@antoineh)

    Not tested thoroughly, but this seems to work:

    
    <?php
    /**
     * Plugin Name: Football Pool New Users
     * Description: Replace UM league text by number when saving the user. No admin approval required for league.
     * Version: 1.1
     * Author: PC1271
     * Author URI: https://wordpress.org/support/users/pc1271/
     * License: MIT
     */
    
    if ( ! defined( 'FPX_LEAGUE_FOR_FREE' ) ) define( 'FPX_LEAGUE_FOR_FREE', 3 );
    if ( ! defined( 'FPX_LEAGUE_FOR_MONEY' ) ) define( 'FPX_LEAGUE_FOR_MONEY', 2 );
    
    // Save this plugin in the wp-content/plugins folder and activate it //
     
    class FootballPoolSetLeagueOnRegistration {
    	public static function init_extension() {
    		// Display a message if the Football Pool plugin is not activated.
    		if ( ! class_exists( 'Football_Pool' ) ) {
    			add_action( 'admin_notices', array( __CLASS__, 'no_fp_plugin_error' ) );
    			return;
    		}
    		
    		// Users are automatically added, so after the save we just set the player in the chosen pool.
    		// Admin approval is not needed with this plugin.
    		add_action( 'footballpool_new_user', array( __CLASS__, 'set_league' ), 50, 2 );
    	}
    	
    	public static function no_fp_plugin_error() {
    		echo '<div class="error"><p>The Football Pool plugin is not activated. Make sure you activate it so the Football Pool extension plugin has some use.</p></div>';
    	}
    	 
    	public static function set_league( $user_id, $league ) {
    		global $wpdb;
    		$prefix = FOOTBALLPOOL_DB_PREFIX;
    		 
    		$pool = new Football_Pool_Pool();
    		if ( $pool->has_leagues ) {
    			switch ( $league ) {
    				case 'for free':
    					$league = FPX_LEAGUE_FOR_FREE;
    					break;
    				case 'for money':
    					$league = FPX_LEAGUE_FOR_MONEY;
    					break;
    				default:
    					$league = FOOTBALLPOOL_LEAGUE_DEFAULT;
    			}
    			update_user_meta( $user_id, 'footballpool_registeredforleague', $league );
    			update_user_meta( $user_id, 'footballpool_league', $league );
    			Football_Pool::update_user_custom_tables( $user_id, $league );
    		} else {
    			$pool->update_league_for_user( $user_id, 0 );
    		}
    	}
    }
     
    add_filter( 'plugins_loaded', array( 'FootballPoolSetLeagueOnRegistration', 'init_extension' ) );
    

    Hi Antoine,

    Thanks for revising my code.
    It took me awhile to test it but unfortunately, it doesn’t seem to work on my installation. I still don’t get to see that a new users chose to be in the paid pool.

    If it’s more convenient for you to test, I can provide you with an admin account on my website.

    And yes, it would be great if UM would support the name-value feature. I would contact them to add a feature request, but I’m unsure how to make them understand what I want this feature to do. This is getting a bit too technical for me.

    Plugin Author AntoineH

    (@antoineh)

    Sure. I can check on your site. My email address can be found on the help page in the admin.

    Thx Antoine,

    For helping me resolve this issue.
    I could have known that translating the strings would suffice to get this working.

    Anyway, I’m glad we can now close this issue.

    Best regards,
    Patrick

Viewing 12 replies - 1 through 12 (of 12 total)
  • You must be logged in to reply to this topic.