I have a function which checks if $_POST is set (a form which submits to the same page). I need to have it initialized before the page is loaded because the function will do a header redirect if the validation goes through without errors.

So the function is registered like this:

add_action('init', 'signup_validate_insert');

And the function itself starts like this:

function signup_validate_insert(){
    $errors = false;
        if (!empty($_POST['submit_msg'])) {
         //validation and stuff

And if there are errors, it returns the errors array:

return $errors;

When the submit button is pressed, the function registers it as it should, and goes through the validation. I have checked to see that the errors array is being filled in the function. But the variable is not returned! (although I have set "return $errors")

The variable is supposed to be returned to the template, where the errors can be displayed if $errors are not empty.

I have tried switching to add_filter instead, with the same result.

So the question is really how to be able to return this variable - hope you can help!

  • Per the docs on add_action(), it will always return true.
    – Pat J
    Oct 28, 2015 at 13:06

3 Answers 3


actions do not "return" values. If you need to do some heavy computation that you want to avoid redoing at a later stage then you should store the value at a global variable (pure global, static or a field in an object depending on your favorite coding style).


See my updated answer, now i tried as per your condition and my url is like now


add_action('init', 'my_cookie');

add_action('init', 'signup_validate_insert');

function signup_validate_insert() {
    if(isset($_REQUEST['query'])) {     
        $errors = 'my_error';
        setcookie("my_cookie", $errors, time()+3600);       

function my_cookie() {
    if(isset($_COOKIE['my_cookie'])) {

        echo $_COOKIE['my_cookie'];

    it is for me working nice. am i right? Reply your thoughts.
  • How do you return the $errors variable?
    – Galivan
    Oct 28, 2015 at 13:57
  • Yes, that could be a workaround...this is not easy. Thanks for the idea!
    – Galivan
    Oct 28, 2015 at 14:08
  • this time i set cookie working like a charm. you try and let me know.
    – Balas
    Oct 28, 2015 at 14:14

If I remember correctly, unless you specify a POST submission, it will use GET as default.
So using $_POST will return empty. If you use $_GET it will work. I often use $_REQUEST to avoid this issue.

As Pat comments, add_action always returns true. So if you need to return erros to show this in the form, then you should consider sending the form via ajax.

function signup_validate_insert()
    $errors = false;
    if (!empty($_POST['submit_msg'])) {
      // Do your validation
    } else {
      $errors = true;

    //If no errors are found, procede with form submition.
      wp_redirect( $location, $status );
    } else {          
        If errors are found you have 3 options:
        1. Store the errors in a db table and retrieve the errors in the page template  
        2. Store the errors in a cookie
        3. Redirect page to form and put the erros in the URL


I would however strongly recommend you to submit the for via jQuery and return errors if any. If not, redirect data to the page to ahndle the form submission.

var formData = $('form').serialize()

  type: "POST",
  url: url,
  data: formData ,
  success: success,
  dataType: dataType
  • Maybe I was unclear in some way. The errors array is being filled in the validation process. So if $_POST['submit_msg'] is not empty, the validation starts, one or more errors are added to the errors array, and then I would like to make this variable available to the template where the form is. I can't find any information on this.
    – Galivan
    Oct 28, 2015 at 14:06
  • See my updated answer
    – Steven
    Oct 28, 2015 at 14:28
  • Yes, it seems that jQuery is the only possible way.. even if I put the validation code inside the template, I can't do a header redirect from there, since part of the page has already been loaded.
    – Galivan
    Oct 28, 2015 at 15:59
  • Unless I have no other choice, I always post form data using jQuery / Ajax. I don't believe storing the data in a global variable is a good way.
    – Steven
    Oct 28, 2015 at 16:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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