So i'm making a extension plugin FOR another plugin. What i want to achieve is that i upon registration they are required to add their Firstname and Lastname, so i added the fields for it, i did the errorchecks for it and everything worked when testing.
But when adding the last step the actual updating the database when i click the register button i get critical errors, the user is added to the database, but no first or lastname. I assume i don't even get in the "function registration_save_user".
Could someone help me on assisting me with my final hurdle, keep in mind i'm very new at all this so any improvements are welcomed. Thanks in advance.
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 );
add_action( 'user_register', [__CLASS__, 'registration_save_user'], null, 4 );
}
//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> voer 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;
}
//3. Save userdata
public static function registration_save_user( $user_id , $first_name, $last_name) {
if ( ! empty( $_POST['first_name'] ) && ! empty( $_POST['last_name'] )) {
update_user_meta( $user_id, 'first_name', $first_name );
update_user_meta( $user_id, 'last_name', $last_name );
}
}
}