admin管理员组文章数量:1125008
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 );
}
}
}
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 );
}
}
}
Share
Improve this question
edited Feb 9, 2024 at 23:59
Pixelhouse
asked Feb 9, 2024 at 23:59
PixelhousePixelhouse
11 bronze badge
1
- What are the errors you get, those usually help diagnose the issue. Can you add them to your question? – Tony Djukic Commented Feb 14, 2024 at 16:02
1 Answer
Reset to default 0The user_register
action hook only passes 2 parameters and your callback requires 3, which will result in a PHP fatal error. You should instead be using the insert_custom_user_meta
filter like so:
class FootballPoolRegisterNames {
public static function init_extension() {
...
add_filter( 'insert_custom_user_meta', [ __CLASS__, 'insert_custom_user_meta' ], 10, 4 );
}
...
public static function insert_custom_user_meta( $custom_meta, $user, $update, $userdata ) {
$custom_meta[ 'first_name' ] = sanitize_text_field( $_POST[ 'first_name' ] ?? '' );
$custom_meta[ 'last_name' ] = sanitize_text_field( $_POST[ 'last_name' ] ?? '' );
return $custom_meta;
}
}
本文标签: pluginsadding firstname and lastname on registration
版权声明:本文标题:plugins - adding firstname and lastname on registration 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736655217a1946236.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论