• Hello again, i’m trying to solve my issue but i don’t think it’s currently possible, but wanted to verify.


    First what i did was write a couple of functions in functions.php to add the standard first-name & last-name fields in my register form. This worked but as you might know, they get added at the back of the form below everything else. By using add_action( ‘register_form’, ‘custom_register_form’ );
    I’m looking for something to add to infront or below your dropdown selector for pools.


    Now i tried looking for do_action or apply_filters in the php files and cannot seem to find anything that would help me other then a function you called registration_form_extra_fields which is exactly where i would want to add below or above.


    Is there a hook that i missed where i would be able to make a custom plugin for your plugin that would allow me to address that area?


    Thanks in advance!

    • This topic was modified 2 years, 3 months ago by pixelhouse18.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter pixelhouse18

    (@pixelhouse18)

    Here’s a little update, i found a hook where i can apply both textfields in a seperate plugin, the layout works, but i cannot get error codes to appear.
    I tried adapting from your code, but it’s still very advanced for me and everything gets me to a critical error.

    Any help would be appreciated.
    Link to code https://pastecode.io/s/ujhh381n
    Thanks in advance.

    Plugin Author AntoineH

    (@antoineh)

    For the error codes you need to hook into the filter ‘registration_errors’. I added it in your code.

    <?php
    /**
     * Plugin Name: Football Pool Register First & Last name
     * Description: Add 2 obligated fields where people need to add their name.
     * Version: 1.0
     * Author: Juanito Clyncke
     * Author URI: mailto:[email protected]
     * License: MIT
     */
    
    // Save this plugin in the wp-content/plugins folder and activate it //
    
    add_filter( 'plugins_loaded', array( 'FootballPoolRegisterNames', 'init_extension' ) );
    
    class FootballPoolRegisterNames {
    	public static function init_extension() {
    		add_action( 'register_form', [__CLASS__, 'registration_form_extra_names'], null, 2 );
    		add_filter( 'registration_errors', [__CLASS__, 'registration_check_fields'], null, 3 );
    	}
    	
    	//1. Add fields. Just the basic layout added before the dropdown.
    	public static function registration_form_extra_names() {
    		$first_name = ( ! empty( $_POST['first_name'] ) ) ? sanitize_text_field( $_POST['first_name'] ) : '';?>
            <p>
                <label for="first_name">Voornaam</label>
                <input type="text" name="first_name" id="first_name" class="input" value="<?php echo esc_attr(  $first_name  ); ?>" size="25" autocomplete="voornaam" required="required">
            </p>
            <?php
        	$last_name = ( ! empty( $_POST['last_name'] ) ) ? sanitize_text_field( $_POST['last_name'] ) : '';?>
            <p>
                <label for="last_name">Achternaam</label>
                <input type="text" name="last_name" id="last_name" class="input" value="<?php echo esc_attr(  $last_name  ); ?>" size="25" autocomplete="achternaam" required="required">
            </p>
            <?php
    	}
    
    	//2. Check fields
    	private static function check_field( &$errors, $field, $message ) {
    		if ( Football_Pool_Utils::post_string( $field ) === '' ) {
    			$errors->add( "{$field}_error", "<strong>Fout:</strong> vul je {$message} in." );
    		}
    	}
    
    	public static function registration_check_fields( $errors ) {
    		// Check the added fields
    		self::check_field( $errors, 'first_name', 'voornaam' );
    		self::check_field( $errors, 'last_name', 'achternaam' );
    
    		return $errors;
    	}
    
    }
    

    Next step would be to add a hook to the action that saves the user data (‘user_register‘), and then do a call to wp_update_user. E.g. something like this:

    wp_update_user( [
        'ID' => $user_id, // this is the ID of the user you want to update.
        'first_name' => $first_name,
        'last_name' => $last_name,
    ] );

    Hope this helps.

    p.s. I removed the translations because there is no use for it when you don’t set a domain.

    Thread Starter pixelhouse18

    (@pixelhouse18)

    Hey AntoineH!
    Thanks for the tips in the right direction, your code worked indeed, but i searched a few hours and could not get it to savethe first and last name(it did save everything else). After about 4/5 hours looking and testing i gave up and watched a movie, until suddenly it came to me, I have access to ChatGPT!

    So i thought might as well give it a try, and what do you know it spit out a perfect working function based on the code we already made.
    It uses “update_user_meta” though instead of the provided “wp_update_user” which i think makes sense since both are stored as meta data in the database in a separate table. Either way it works like a charm now! You can see the wonderful interaction below lol.

    I thought i’d share it with you since it might be usefull for other people someday who decide they also want to add first/last names as required fields. I added your name/email back in the plugin since this was mostly you and ChatGPT and it’s your wonderfull plugin, you deserve all the credit.

    Link to php code.

    • This reply was modified 2 years, 3 months ago by pixelhouse18.
    Plugin Author AntoineH

    (@antoineh)

    Oh wow, that is absolutely awesome. I’ve used ChatGPT in several occasions for rewriting texts, but not yet for development. This is quite amazing and very handy indeed.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Adding more textfields to register form with hooks?’ is closed to new replies.