0

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?

1

You need a filter not an action. Something like this (notes are commented into the code. You have a few PHP errors that you need to correct):

function signup_validate_insert($post){
  $errors = false;
  if (isset($post['submit_msg'])) {
    // validate , insert into database
  }
  return $errors;
}
add_filter('signup_insert', 'signup_validate_insert');
$errors = apply_filters('signup_insert', $_POST);

if((!empty($errors))){ // $errors is going to be set; use empty() ?>
  <div class="errors"><?php 
    foreach($errors as $error) { ?>
      <p class='errortext'><?php 
        echo $error; // echo was missing; you would have no output ?> 
      </p><?php 
    } ?>
  </div><?php
}

Filters return content. Actions do stuff. I you want an action echo directly from your callback:

function signup_validate_insert($post){
  $errors = false;
  if (isset($post['submit_msg'])) {
    // validate , insert into database
  }
  if((!empty($errors))){ // $errors is going to be set; use empty() ?>
    <div class="errors"><?php 
      foreach($errors as $error) { ?>
        <p class='errortext'><?php 
          echo $error; // echo was missing; you would have no output ?> 
        </p><?php 
      } ?>
    </div><?php
  }
}
add_action('signup_insert', 'signup_validate_insert');
do_action('signup_insert', $_POST);

Honestly, what you are doing may be overly complicated and the whole thing might best be written without the hook at all. Do you need this to be extensible by a third party?

Try looking over: Clarification on filters and hooks Difference Between Filter and Action Hooks?

  • I tried the filter , but there's a problem with adding $_POST with "apply_filters" because it means that it is set when it isn't. Or if I add $_POST['submit_msg'] (which is the submit button) , then submit_msg becomes undefined. Maybe I have to put the validation on the template part, but then it isn't strictly a template...I would like to do it in a nice way if possible. – Galivan Oct 23 '15 at 14:38
  • $post['submit_msg'] won't be set if it isn't. That doesn't make sense. $post will be set all the time, but not $post['submit_msg'], which is what the logic works on. Did you try this? – s_ha_dum Oct 23 '15 at 14:45
  • Alright, I did a mistake, but I fixed it with "if (!empty($post['submit_msg']))" on the beginning of my function. But it doesn't return the $errors array (errors is undefined). I've heard that filters can return a variable, but I can't add the $errors as an argument because it becomes undefined initially. – Galivan Oct 23 '15 at 14:50
  • Please look what I did. I am not passing in $errors. I am passing in the data to be validated-- the $_POST data. See the edit. Just initialize $errors at the top of the function. – s_ha_dum Oct 23 '15 at 14:59
  • WOW it seems to work now! I have to leave the office now, so I can't test it thouroghly but it does seems to work. Thanks a LOT!! – Galivan Oct 23 '15 at 15:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.