Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

extract($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?

share|improve this question
    
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 Dec 18 '11 at 12:42
add comment

1 Answer

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.

share|improve this answer
    
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 Sep 18 '11 at 8:47
    
A simply create a new function inside that class and call it. –  Bainternet Sep 18 '11 at 9:29
    
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 Sep 19 '11 at 6:51
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.