I have been working with widgets and widgets API.

I see that the widget form is rendered and processed through widget() function. I can write validation logic inside the widget() function. I will do a isset($_POST) check and write the form handling logic in it.

Something like this:

function widget($args){


is isset(){$_POST['fieldname']

{  // form processing logic  }

// echo HTML Form


I would like to separate the form processing logic from the widget function. How can I achieve this? Is there a standard way for doing this?

  • The update method inside your widget class should deal with validating and sanitizing the data, the widget method should deal with preparing that data for display.
    – t31os
    Commented Dec 18, 2011 at 12:42

1 Answer 1


If you are using the Widget API then declare your widget as a class which extends WP_Widget class and there you can define an upadte function where you can do your validation, the codex as a nice example of doing just that.

  • I am using the Widget API. As far as I understand , the update function handles the widget settings.. i.e the input entered via the admin interface. I am developing a newsletter widget where a user can enter his email and subscribe to the blog. I handle the subscription logic , email-validation etc. inside the widget() function and it makes the code messy. Is there a way to separate subscription logic, validation from the widget() function?
    – Ankit
    Commented Sep 18, 2011 at 8:47
  • A simply create a new function inside that class and call it.
    – Bainternet
    Commented Sep 18, 2011 at 9:29
  • 1
    Thanks.. will try that. Seems like Wordpress does not provide a standard way of doing this. Have been searching for it but couldn't find anything.
    – Ankit
    Commented Sep 19, 2011 at 6:51

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.