你不需要打电话\'pre_user_*\'
过滤器,由WordPress内部调用wp_insert_user
.
我还建议使用phpfilter_input
或filter_input_array
清理表单输入。
示例代码:
$args = array(
\'pwd1\' => FILTER_SANITIZE_STRING,
\'pwd2\' => FILTER_SANITIZE_STRING,
\'first_name\' => FILTER_SANITIZE_STRING,
\'last_name\' => FILTER_SANITIZE_STRING,
\'username\' => FILTER_SANITIZE_STRING,
\'email\' => FILTER_SANITIZE_STRING
);
$form_data = filter_input_array( INPUT_POST, $args, TRUE );
$keys = array_keys($args);
$required = TRUE;
while ( ! empty( $keys ) && $required ) {
$key = array_shift( $keys );
if ( empty( $form_data[$key] ) ) $required = FALSE;
}
if( ! $required ) {
$err = \'Please don\\\'t leave the required fields.\';
} else if( ! filter_var( $form_data[\'email\'], FILTER_VALIDATE_EMAIL ) ) {
$err = \'Invalid email address.\';
} else if( $form_data[\'pwd1\'] !== $form_data[\'pwd2\'] ){
$err = \'Password do not match.\';
} else {
$user_data = array(
\'user_login\' => $form_data[\'username\'],
\'user_pass\' => $form_data[\'pwd1\'],
\'user_email\' => $form_data[\'email\'],
\'role\' => \'subscriber\'
);
$user_id = wp_insert_user( $user_data );
if( is_wp_error( $user_id ) ) {
$err = \'Error on user creation: \' . $user_id->get_error_message();
} else {
do_action(\'user_register\', $user_id);
$success = \'You\\\'re successfully register\';
}
}