I have a form in a template that upon submission should be validated, and any errors should be displayed, and if no errors, insert the data into the database.
I'm new to wordpress, but my idea was that maybe an "action" could solve this. So I have a function in functions.php:
function signup_validate_insert($errors){
if (isset($_POST['submit_msg'])) {
// validate , insert into database
}
return $errors;
}
and I have the "add action" on the functions.php:
add_action('signup_insert', 'signup_validate_insert');
The form is actually in a template part which is added to a template page (with " get_template_part").
In the template part I have added the action:
<?php do_action('signup_insert', $errors); ?>
The form is sent to the same page on submit.
There is a div for displaying errors:
<div class="errors">
<?php if((isset($errors))){
foreach($errors as $error): ?>
<p class='errortext'> <?php $error ?> </p>
<?php endforeach ?>
<?php }else if(!isset($errors)){
} ?>
</div>
The action function registers that "$_POST" is set on submit. But it doesn't return the "$errors" array, because I get "Undefined variable: errors" in my form page. So I wonder how I get this function to return this variable?