I have a custom registration form that I insert into a page via shortcode. The shortcode function looks like this:
function custom_registration_form_shortcode() {
$error = FALSE;
if (isset($_POST["submit"])) {
//Get other $_POST data here
$email = is_email($_POST["email"]);
if (!$email) {
$error = TRUE;
$errorMsg = "EMAIL NOT VALID";
}
if (email_exists($email)) {
$error = TRUE;
$errorMsg = "EMAIL EXISTS";
}
}
if (!$error) {
// Creating random password, setting username, wp_create_user, etc. working fine
$output = "GOOD";
}
// Some other stuff here that does not set $error to TRUE
if ($error) {
$output = $errorMsg;
}
return $output;
}
function jogol_add_shortcodes() {
add_shortcode('jogol-registration-form', 'custom_registration_form_shortcode');
}
add_action('init', 'jogol_add_shortcodes');
What happens is that the user gets registered fine, but it still returns “EMAIL EXISTS” as if wp_create_user gets executed before email_exists($email) or the code runs twice on submit.
I´m going nuts and any help/hints are highly appreciated. Thanks!
UPDATE:
Here´s a stripped down version. No validation. Just for testing purposes.
function registration_form_shortcode() {
if (isset($_POST["registration_submit"])) {
if (email_exists($_POST["email"])) {
$output = "EMAIL EXISTS";
} else {
$username = $_POST["email"];
$random_password = wp_generate_password(8, false);
$status = wp_create_user($username, $random_password, $_POST["email"]);
$output = "GOOD";
}
}
$form =
'<form name="registration" action="'.esc_url($_SERVER['REQUEST_URI']).'" method="post">
<label for="email">Email</label>
<input type="text" name="email" value="'.$_POST["email"].'" />
<input type="submit" name="registration_submit" value="Send" />
</form>';
$output .= $form;
return $output;
}
function add_my_shortcodes() {
add_shortcode('registration-form', 'registration_form_shortcode');
}
add_action('init', 'add_my_shortcodes');
What I did (and ask you to try please):
- Put that code in my theme´s functions.php
- Added the shortcode to a page via [registration-form]
What it should do when I enter a valid email-address that is not already registered with my WP-install and hit the Send-button:
- Create the user
- Display “GOOD” above the form
What it actually does:
- Create the user
- Display “EMAIL EXISTS” above the form
I have no clue. Any ideas?