Hi I have created a form function code inside a theme functions.php file
my file is https://example.com/counsellor-registration/
which is showing html layout of form when i open this link .
problem is that after submitting form 404 error coming on same page
my code is
function custom_counsellor_registration_form() {
ob_start();
$form_action = esc_url(trailingslashit(get_permalink()));
error_log('Counsellor Form Action URL: ' . $form_action);
if (isset($_POST['counsellor_registration_submit'])) {
error_log('Counsellor Form Submitted. POST Data: ' . print_r($_POST, true));
$name = sanitize_text_field($_POST['name']);
$designation = sanitize_text_field($_POST['designation']);
$qualification = sanitize_text_field($_POST['qualification']);
$time_slots = isset($_POST['time_slots']) ? array_map('sanitize_text_field', $_POST['time_slots']) : array();
$specification = sanitize_textarea_field($_POST['specification']);
$username = sanitize_text_field($_POST['username']);
$email = sanitize_email($_POST['email']);
$password = $_POST['password'];
$agreement = isset($_POST['agreement']) ? 1 : 0;
$errors = [];
if (empty($username) || empty($email) || empty($password)) {
$errors[] = "Username, email, and password are required.";
}
if (!is_email($email)) {
$errors[] = "Please enter a valid email address.";
}
if (strlen($password) < 6) {
$errors[] = "Password must be at least 6 characters long.";
}
if (empty($errors) && $agreement) {
$user_id = wp_create_user($username, $password, $email);
if (!is_wp_error($user_id)) {
wp_update_user(array('ID' => $user_id, 'role' => 'counsellor'));
update_user_meta($user_id, 'name', $name);
update_user_meta($user_id, 'designation', $designation);
update_user_meta($user_id, 'qualification', $qualification);
update_user_meta($user_id, 'time_slots', $time_slots);
update_user_meta($user_id, 'specification', $specification);
wp_redirect(wp_login_url() . '?registration=success');
exit;
} else {
error_log('Counsellor Registration Error: ' . $user_id->get_error_message());
$errors[] = $user_id->get_error_message();
}
}
if (!empty($errors)) {
echo '<div class="message" style="color:red;">';
foreach ($errors as $error) {
echo '<p>' . esc_html($error) . '</p>';
}
echo '</div>';
} else if (!$agreement) {
echo '<p class="message" style="color:red;">Please agree to the terms to proceed.</p>';
}
}
?>
<div class="registration-form">
<h2>Counsellor Registration</h2>
<form action="" enctype="multipart/form-data" method="POST">
<p>
<label for="name">Name:</label><br>
<input type="text" name="name" id="name" value="<?php echo isset($_POST['name']) ? esc_attr($_POST['name']) : ''; ?>" required>
</p>
<p>
<label for="designation">Designation:</label><br>
<input type="text" name="designation" id="designation" value="<?php echo isset($_POST['designation']) ? esc_attr($_POST['designation']) : ''; ?>" required>
</p>
<p>
<label for="qualification">Qualification:</label><br>
<input type="text" name="qualification" id="qualification" value="<?php echo isset($_POST['qualification']) ? esc_attr($_POST['qualification']) : ''; ?>" required>
</p>
<p>
<label>Time slot comfortable for you:</label><br>
<div class="time-slot">
<input type="checkbox" name="time_slots[]" value="9 AM - 12 PM" <?php echo isset($_POST['time_slots']) && in_array('9 AM - 12 PM', $_POST['time_slots']) ? 'checked' : ''; ?>> 9 AM - 12 PM<br>
<input type="checkbox" name="time_slots[]" value="12 PM - 3 PM" <?php echo isset($_POST['time_slots']) && in_array('12 PM - 3 PM', $_POST['time_slots']) ? 'checked' : ''; ?>> 12 PM - 3 PM<br>
<input type="checkbox" name="time_slots[]" value="3 PM - 6 PM" <?php echo isset($_POST['time_slots']) && in_array('3 PM - 6 PM', $_POST['time_slots']) ? 'checked' : ''; ?>> 3 PM - 6 PM<br>
<input type="checkbox" name="time_slots[]" value="6 PM - 9 PM" <?php echo isset($_POST['time_slots']) && in_array('6 PM - 9 PM', $_POST['time_slots']) ? 'checked' : ''; ?>> 6 PM - 9 PM<br>
</div>
</p>
<p>
<label for="specification">Any specification:</label><br>
<textarea name="specification" id="specification"><?php echo isset($_POST['specification']) ? esc_textarea($_POST['specification']) : ''; ?></textarea>
</p>
<p>
<label for="username">Username:</label><br>
<input type="text" name="username" id="username" value="<?php echo isset($_POST['username']) ? esc_attr($_POST['username']) : ''; ?>" required>
</p>
<p>
<label for="email">Email ID:</label><br>
<input type="email" name="email" id="email" value="<?php echo isset($_POST['email']) ? esc_attr($_POST['email']) : ''; ?>" required>
</p>
<p>
<label for="password">Password:</label><br>
<input type="password" name="password" id="password" required>
</p>
<p>
<input type="checkbox" name="agreement" id="agreement" required <?php echo isset($_POST['agreement']) ? 'checked' : ''; ?>>
<label for="agreement">I agree all the details given above are true to the best of my knowledge</label>
</p>
<p>
<input type="submit" name="counsellor_registration_submit" value="Register">
</p>
</form>
</div>
<?php
return ob_get_clean();
}
add_shortcode('custom_counsellor_registration', 'custom_counsellor_registration_form');
I have also tried with <form method="post" action="<?php echo esc_url(trailingslashit(get_permalink())); ?>">
when I submit form my headers shows
scheme
https
host
example.com
filename
/counsellor-registration/
Status
200
my request goes
------geckoformboundarya1c41156493fa0c19c973b6717e255d9
Content-Disposition: form-data; name="name"
testing name
------geckoformboundarya1c41156493fa0c19c973b6717e255d9
Content-Disposition: form-data; name="designation"
MD
------geckoformboundarya1c41156493fa0c19c973b6717e255d9
Content-Disposition: form-data; name="qualification"
MBBS
------geckoformboundarya1c41156493fa0c19c973b6717e255d9
Content-Disposition: form-data; name="time_slots[]"
12 PM - 3 PM
------geckoformboundarya1c41156493fa0c19c973b6717e255d9
Content-Disposition: form-data; name="specification"
klklkl
------geckoformboundarya1c41156493fa0c19c973b6717e255d9
Content-Disposition: form-data; name="username"
counc01
------geckoformboundarya1c41156493fa0c19c973b6717e255d9
Content-Disposition: form-data; name="email"
[email protected]
------geckoformboundarya1c41156493fa0c19c973b6717e255d9
Content-Disposition: form-data; name="password"
123456
------geckoformboundarya1c41156493fa0c19c973b6717e255d9
Content-Disposition: form-data; name="agreement"
on
------geckoformboundarya1c41156493fa0c19c973b6717e255d9
Content-Disposition: form-data; name="counsellor_registration_submit"
Register
------geckoformboundarya1c41156493fa0c19c973b6717e255d9--