0

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!

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

3 Answers 3

1

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).

0

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

    http://www.example.com/?query=yes

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);       
        wp_redirect(home_url('shortcode-9/'));
        die();
    }
}

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

        echo $_COOKIE['my_cookie'];
    }
}

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

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.
    if(!$errors){
      wp_redirect( $location, $status );
      exit;
    } 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()

$.ajax({
  type: "POST",
  url: url,
  data: formData ,
  success: success,
  dataType: dataType
});
4
  • 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
    Commented Oct 28, 2015 at 14:06
  • See my updated answer
    – Steven
    Commented 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
    Commented 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
    Commented 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.