WordPress.org

Make WordPress Core

Opened 8 years ago

Closed 8 years ago

Last modified 6 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:

Description

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)

comment:1 @rob1n8 years ago

  • Milestone set to 2.4

More up to the user to do this, no?

comment:2 @JeremyVisser8 years ago

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

comment:3 @Otto428 years ago

+1 for letting this be a plugin.

JeremyVisser:
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
}
add_action('activate_pluginname.php','this_only_ever_gets_called_once_on_activation_of_your_plugin');


function do_my_thing_function() {
// do whatever you want to do daily
}
add_action('do_my_thing_action_hook','do_my_thing_function');


function this_gets_called_on_deactivation_of_your_plugin() {
wp_clear_scheduled_hook('do_my_thing_action_hook');
}
add_action('deactivate_pluginname.php','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') )
);
}
add_filter('cron_schedules','add_my_own_schedule');

Pretty straightforward.

comment:4 @JeremyVisser8 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.

comment:5 @JeremyVisser8 years ago

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

comment:6 @rob1n8 years ago

  • Milestone 2.4 deleted

comment:7 @JeremyVisser8 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.