所以我正在为另一个插件制作一个扩展插件。我想要实现的是,我在注册时需要添加他们的名字和姓氏,所以我添加了它的字段,我对其进行了错误检查,并且在测试时一切正常。
但是当添加最后一步实际更新数据库时,当我单击注册按钮时,我收到严重错误,用户被添加到数据库中,但没有名字或姓氏。我想我什至没有进入“功能registration_save_user”。
有人可以帮我解决最后的障碍吗?请记住,我对这一切都很陌生,所以欢迎任何改进。预先感谢。
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 );
}
}
}
已尝试多次,但所有这些都没有名字和姓氏,并且存在严重错误,但所有这些都发布到我的数据库中,所以我假设
add_action( 'user_register', [__CLASS__, 'registration_save_user'], null, 4 );
和/或
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 );
}
}
完全错误。
因此,正如评论中提到的,我让 ChatGPT 读取我的文件并修复有错误的代码(保存到数据库),它更正了我的代码并告诉我我做错了什么。
我在第 7 行的 add_action 部分犯了一个大错误,当尝试定义“user_register”时,它只需要 1 个变量,即我有“NULL,4”的 user_id,因为我不知道更好,而且我很糟糕.
然后它吐出以下函数,该函数与我已经提供的代码完美配合,无需我之前尝试附加的 $first_name 和 $last_name 。
public static function registration_save_user( $user_id ) {
if ( isset( $_POST['first_name'] ) && !empty( $_POST['first_name'] ) ) {
update_user_meta( $user_id, 'first_name', sanitize_text_field( $_POST['first_name'] ) );
}
if ( isset( $_POST['last_name'] ) && !empty( $_POST['last_name'] ) ) {
update_user_meta( $user_id, 'last_name', sanitize_text_field( $_POST['last_name'] ) );
}
}
感谢 ChatGPT,现在一切正常了,哈哈! 谢谢大家。