[wp-hackers] Best way to kick off actual work from plugin settings page?
Dougal Campbell
dougal at gunters.org
Mon Jul 12 15:06:11 UTC 2010
On Jul 12 2010 3:04 AM, Matt Jacob wrote:
> I'm using the settings API for my plugin's custom settings page, and
> it's working pretty well. Sometimes, though, I want to perform an
> action based on a submitted value instead of just saving the value
> back to the DB. Where's the best place to intercept the form
> submission and do some actual work?
>
> I have two possibilities in my mind:
>
> 1. Hook into the admin_init action, since this should come before the
> $_POST array gets wiped out. I can determine if a specific submit
> button was used, if a certain checkbox was checked, etc. and then do
> some work based on those criteria.
>
> 2. Use the validation callback in register_setting() to check the
> submitted fields for what I want, do some work, and then return only
> the options I actually want to be persistent. (I'm using a single
> array for all of my options, so it's easy to leave some options out.)
>
> Option #2 seems more elegant, but it's kind of overloading the purpose
> of the callback.
>
> Just for reference, the kind of "work" I'm talking about doing is
> regenerating a secret token or importing existing records to custom
> posts during an upgrade.
>
I've used both methods before. Which I choose seems to depend on context
and complexity of the tasks I'm performing. Quick, simple things can
often slip into the settings validation callback. For more complex
functions I might catch it on admin_init. Or sometimes I'll use a hybrid
method -- detection in the validation callback, setting a transient
flag, which I then look for later (perhaps for a cron schedule).
--
Dougal Campbell <dougal at gunters.org>
http://dougal.gunters.org/
http://twitter.com/dougal
http://twitual.com/
More information about the wp-hackers
mailing list