Make WordPress Core

Opened 13 years ago

Closed 13 years ago

Last modified 11 years ago

#4278 closed enhancement (wontfix)

OPTIMIZE some TABLES from time to time

Reported by: ozh Owned by:
Milestone: Priority: normal
Severity: trivial Version:
Component: Optimization Keywords: mysql optimize
Focuses: Cc:


Ola there, I have a (minor & trivial) suggestion : from time to time, like when rnd(1,5) == 5, it should do no harm to "OPTIMIZE TABLE wp_options, wp_comments", since there are often data removed from this table (comment spams & rss feeds).

Same thing could apply if a major delete is detected within other tables (posts & categories)

Change History (7)

#1 @rob1n
13 years ago

  • Milestone set to 2.4

More up to the user to do this, no?

#2 @JeremyVisser
13 years ago

If I work out how the WP cron jobs work, I should make a plugin for it some day.

#3 @Otto42
13 years ago

+1 for letting this be a plugin.

It's really not all that difficult to do recurring cron jobs with the new cron code. Here's some example code (may not be 100% correct, but it's pretty close).

function this_only_ever_gets_called_once_on_activation_of_your_plugin() {
wp_schedule_event(time(), 'daily', 'do_my_thing_action_hook'); // no args to be passed in this example

function do_my_thing_function() {
// do whatever you want to do daily

function this_gets_called_on_deactivation_of_your_plugin() {

Essentially what's going on is that you're telling the cron process to, once a day (starting right now, due to the time() call), call some action hook. The action hook can be any text you want, of course. Then, you hook your own functions into those hooks. Args will get passed along to the functions, if you need them. Usually you probably won't need them.

In the above example, I'm adding a recurring hook upon plugin activation and then clearing it on deactivation. This is important: If you fail to clean up after yourself on deactivation, then cron will continually hit that action hook even though nothing is attached to it anymore. This causes a waste of cycles and CPU time.

Another important thing to remember is that you only ever call wp_schedule_event once, ever. It will recur on its own after that. This is why you do it on activation only and not on every load of the plugin.

If you want to schedule a single event at some specific time, use the wp_schedule_single_event() function instead. It takes a time parameter (when the hook will be called upon) and the name of a hook to call. This gives you finer control if you prefer to do it that way. You can even make this recur if you want, by having the function that gets called actually reschedule the same call for the future. This might be a bit safer too, since, in the worst case, you won't leave a recurring hook out there if your deactivation fails for whatever reason (like they just delete the plugin instead of deactivating it).

The cron code has hourly and daily built into it as schedules. But if you want to add your own schedule instead, do this:

function add_my_own_schedule($useless_arg1_is_an_empty_array)
return array(
'fiveminute' => array( 'interval' => 300, 'display' => __('Once Every 5 minutes') ),        'weekly' => array( 'interval' => 604800, 'display' => __('Once Weekly') )

Pretty straightforward.

#4 @JeremyVisser
13 years ago

Wow, thanks for that Otto. That's very clearly explained. Once I get the hang of it, I'll have to write it up on the Codex.

#5 @JeremyVisser
13 years ago

  • Resolution set to wontfix
  • Status changed from new to closed

#6 @rob1n
13 years ago

  • Milestone 2.4 deleted

#7 @JeremyVisser
13 years ago

By the way, I have been trying to get a plugin to do this, but I'm not having much success. I just can't get the hang of cron. :(

Note: See TracTickets for help on using tickets.