Is there a way to access plugin variables from the theme template files without using globals?

I have the following situation:

  • One plugin function validates POST data submitted in a form.
  • If validation fails generates an WP Error
  • If the WP Error is there the template should show it, otherwise it should be normal output.

Here there is an interesting solution using custom filters. However, I am using the same set of functions in different forms, validation rules change between form and the errors are therefore different. I would like to achieve something inline, rather than having to create different callback functions for each of the cases.

In the theme template file:

$errors = apply_filters('form_errors','');
if ($errors != '') {echo $errors;}

I would like to have something like the following in the plugin:

$errors = validate_form($_POST)
// the following line won't work as a callback function is needed.
// Using a callback function would probably not solve the issue, as different
// errors need to be passed depending on which form is validated. 
add_filter('form_errors', $errors);
// the following would work, but how to pass the actual errors as argument to the return_error function?
add_filter('form_errors', 'return_errors'); 

Any ideas to avoid declaring $errors as global?

1 Answer 1


Using add_filter will automatically pass $errors to your callback function:

add_filter('form_errors', 'return_errors');

function return_errors($errors) {
    // validate $errors based on form conditions
    return $errors;

If the filter has more than one variable passed you can access that extra information by adding a filter priority and specifying the number of arguments to pass to the callback function. For example, if the validation function had:

return apply_filters('form_errors', $errors, $posted);

You could use:

add_filter('form_errors', 'return_errors', 10, 2);
function return_errors($errors, $posted) {
    // form validation code using $errors or $posted
    return $errors;

Note the reason you need to set the filter priority explicitly as the third argument in that case is so you can "get to" the fourth argument for add_filter - which again is the number of filter arguments to pass.

  • Thanks majick, not sure whether this solution would work in my setting. $errors is not generated in the theme template prior to calling apply_filters. Form validation is done in the plugin, if there are errors, these should be passed to the template to be displayed. Do you imply using apply_filters in the plugin function and add_filter in the theme? Jul 8, 2017 at 3:13
  • If there is a specific filter from the plugin already, add a callback to that and use it to write errors to a global variable. I know your question says "without a global", but I don't see how you would get them in the template later without re-running the form validation on $_POST all over again.
    – majick
    Jul 8, 2017 at 3:33

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.