0

I am creating a plugin that has a frontend form, which in turn has some validation.. Errors if validation fails and success if it passes. I have tried everything and can't get messages to appear on the frontend.

The frontend html is echoed out through a shortcode, maybe that is the cause?

class My_Plugin {

  private $error;

  public function __construct()
  {
    add_shortcode('my_plugin_form', array($this, 'form_shortcode'));
    add_action('init', array($this, 'form_submission'), 10, 0);
  }

  public function setError($value)
  {
    $this->error = $value;
  }

  public function getError()
  {
    return $this->error;
  }

  public function form_validation()
  {
    // do some form validation
  }

  public function form_submission()
  {
    if(isset($_POST['submit']) && $_POST['action'] == 'my_plugin_form')
    {
      if($this->form_validation() == 'passed')
      {
        // success
      }
      else
      {
        $this->setError('Form validation failed');
      }
    }
  }

  public function form_shortcode()
  {
    $err = $this->getError();

    ob_start();

    // Some HTML

    $this->getError();

    // Some more HTML

    $html = ob_get_contents();
    ob_end_clean();
    return $html;
  }
}
  • where is called "form_validation()" ? – mmm Jul 1 '15 at 20:38
  • I have improved my code. – Goodbytes Jul 1 '15 at 20:43
  • I read the improvement but now, where is called "form_submission" ? – mmm Jul 1 '15 at 20:46
  • whoops, sorry.. updated :) – Goodbytes Jul 1 '15 at 20:58
  • note that WordPress has an error class. also, this may be of interest to you. – Milo Jul 1 '15 at 21:08

Your Answer

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

Browse other questions tagged or ask your own question.