0

I've been developing a wordpress plugin that has an admin section with a form to add and edit various things. Now after an admin submits the form I wanted to redirect if successful (to show updated values) or show the various error messages if not. I got the header errors from trying wp_redirect and after looking through SO I started to use the add_action method so I could redirect without header error messages. The problem I have now though is that my array of errors is now always null, even if the form wasn't submitted correctly. I have defined the errors variable before add_action and in the function in add_action I have

   global $errors;

If I do a var_dump of $errors in the function that handles the submit it is populated, but then on my actual page it is always empty. My guess is that I'm not familiar with the order the pages get called in WP and I'm missing something, but how does anyone else handle this?

  //the index.php for pluging
  //require necessary files
  global $errors = null;
  include('plugin_file.php');
  //Tie into wordpress hooks
  add_action('widgets_init', 'plugin::register_func');
  add_action('admin_menu', 'plugin::add_menu_item');
  add_action('admin_init', 'plugin::check_form_submission');

Then the form submission is similar to

  //THE Class file for my plugin
  static function check_form_submission(){
  global $errors;
        if(empty($_POST['some_field'])){
                      $errors['some-error'] = 'some error';
        }
  if(!$errors){
    //handle and redirect here
   }else{
  var_dump($errors); // does have values
  }
  }

In the actual admin page that shows the form

  //actual admin page that shows in WP
  var_dump($errors); // returns NULL

will always be null even if it has values above. Anyone got any advice how to handle this? I'm new to WP plugin development so not sure what the best practice is.

1 Answer 1

1

well it looks like I worked this out.

On the actual admin page I need to declare my $errors array as global as well

global $errors;

I guess wordpress must somehow box the admin page into a function and so the $errors array was not referencing the global var.

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.