(第1个职位)
我已经为我的Wordpress站点构建了一个自定义注册表单,尽管它具有完整的功能,但对于username\\u exists和email\\u exists,我都会收到验证错误。
事情就是这样:
如果我填写的注册表有任何错误,它不会注册,并且会产生正确的错误。
如果我填写的注册表没有错误(以及唯一的用户名和电子邮件),它会正确注册并插入用户数据。但它仍然会生成username\\u exists和email\\u exists错误。
几周来,我一直在努力解决这个问题,但一直没有找到解决方案。如果需要,我很乐意提供代码。
非常感谢。
(主插件代码-如果格式不对,很抱歉。仍在习惯论坛)
function slyd_register() {
wp_enqueue_style(\'slyd-register\', plugins_url(\'/css/sr-styles.css\', __FILE__));
wp_enqueue_script( \'slyd_register\', plugins_url( \'/js/sr-scripts.js\', __FILE__ ));
}
add_action(\'wp_enqueue_scripts\',\'slyd_register\');
/////////////////
// PLUGIN CORE //
/////////////////
function cr(&$fields, &$errors) {
// Check args and replace if necessary
if (!is_array($fields)) $fields = array();
if (!is_wp_error($errors)) $errors = new WP_Error;
// Check for form submit
if (isset($_POST[\'reg_submit\'])) {
// Get fields from submitted form
$fields = cr_get_fields();
// Validate fields and produce errors
if (cr_validate($fields, $errors)) {
// If successful, register user
wp_insert_user($fields);
echo \'\';
echo \'Registration complete. Please Login\';
echo \'\';
// Clear field data
$fields = array();
}
}
// Santitize fields
cr_sanitize($fields);
// Generate form
cr_display_form($fields, $errors);
}
function cr_sanitize(&$fields) {
$fields[\'user_role\'] = isset($fields[\'user_role\']) ? sanitize_text_field($fields[\'user_role\']) : \'\';
$fields[\'user_login\'] = isset($fields[\'user_login\']) ? sanitize_user($fields[\'user_login\']) : \'\';
$fields[\'user_email\'] = isset($fields[\'user_email\']) ? sanitize_email($fields[\'user_email\']) : \'\';
$fields[\'user_pass\'] = isset($fields[\'user_pass\']) ? esc_attr($fields[\'user_pass\']) : \'\';
$fields[\'user_pass2\'] = isset($fields[\'user_pass2\']) ? esc_attr($fields[\'user_pass2\']) : \'\';
$fields[\'first_name\'] = isset($fields[\'first_name\']) ? sanitize_text_field($fields[\'first_name\']) : \'\';
$fields[\'last_name\'] = isset($fields[\'last_name\']) ? sanitize_text_field($fields[\'last_name\']) : \'\';
$fields[\'nickname\'] = isset($fields[\'nickname\']) ? sanitize_text_field($fields[\'nickname\']) : \'\';
}
function cr_display_form($fields = array(), $errors = null) {
// Check for wp error obj and see if it has any errors
if (is_wp_error($errors) && count($errors->get_error_messages()) > 0) {
// Display errors
foreach ($errors->get_error_messages() as $key=>$val) {
echo \'\';
echo \'\' . $val . \'\';
echo \'\';
}
}
global $wp_roles;
if (!is_user_logged_in()) {
?>
(MAIN FORM HERE)
$_POST[\'user_role\'],
\'user_login\' => $_POST[\'user_login\'],
\'user_email\' => $_POST[\'user_email\'],
\'user_pass\' => $_POST[\'user_pass\'],
\'user_pass2\' => $_POST[\'user_pass2\'],
\'first_name\' => $_POST[\'first_name\'],
\'last_name\' => $_POST[\'last_name\'],
\'nickname\' => $_POST[\'nickname\']
);
}
function cr_validate(&$fields, &$errors) {
// Make sure there is a proper wp error obj
// If not, make one
if (!is_wp_error($errors)) $errors = new WP_Error;
//role
if ( empty( $fields[\'user_role\'] ) || ! empty( $fields[\'user_role\'] ) && trim( $fields[\'user_role\'] ) == \'\' ) {
$errors->add(\'role\',\'ERROR: You must include a plan\');
}
//make sure fields are not empty
$fieldVal = array($fields[\'user_email\'], $fields[\'first_name\'], $fields[\'last_name\'], $fields[\'nickname\']);
if (in_array(\'\',$fieldVal)) {
$errors->add(\'field\', \'Missing Entry: All fields are required\');
}
//username
if (strlen($fields[\'user_login\']) add(\'username_length\', \'Username too short. At least 4 characters are required\');
}
if (username_exists($fields[\'user_login\'])) {
$errors->add(\'user_name\', \'The username is already in use\');
}
if (!validate_username($fields[\'user_login\'])) {
$errors->add(\'username_invalid\', \'Sorry, the username you entered is not valid\');
}
//email
if (!is_email($fields[\'user_email\'])) {
$errors->add(\'email_invalid\', \'This is not a valid email address\');
}
if (email_exists($fields[\'user_email\'])) {
$errors->add(\'email\', \'This email address has already been used\');
}
//password
if (strlen($fields[\'user_pass\']) add(\'password\', \'Password must be 8 or more characters\');
}
if (isset($fields[\'user_pass\']) && $fields[\'user_pass\'] != $fields[\'user_pass2\'] ) {
$errors->add(\'password_reset_mismatch\', \'The passwords do not match\');
}
// If errors were produced, fail
if (count($errors->get_error_messages()) > 0) {
return false;
}
// Else, success!
return true;
}
///////////////
// SHORTCODE //
///////////////
// The callback function for the [cr] shortcode
function reg_shortcode() {
$fields = array();
$errors = new WP_Error();
// Buffer output
ob_start();
// Custom registration, go!
cr($fields, $errors);
// Return buffer
return ob_get_clean();
}
add_shortcode(\'ss_registration_form\', \'reg_shortcode\');