Add Snippet To Project
Have you ever made changes to your WordPress site’s permalink structure and found that those changes weren’t immediately reflected in your URLs? This can be frustrating, especially if you rely on SEO and want to ensure your site’s URLs are properly indexed by search engines. In this article, we’ll explore a solution to this problem by implementing a cron job that automatically flushes the permalinks in WordPress once per hour, ensuring that any changes to your permalink structure take effect in a timely manner.
function wpturbo_flush_permalinks_cron() {
if ( ! wp_next_scheduled( 'wpturbo_flush_permalinks_event' ) ) {
wp_schedule_event( time(), 'hourly', 'wpturbo_flush_permalinks_event' );
}
}
add_action( 'wp', 'wpturbo_flush_permalinks_cron' );
function wpturbo_flush_permalinks() {
flush_rewrite_rules();
}
add_action( 'wpturbo_flush_permalinks_event', 'wpturbo_flush_permalinks' );
The provided code snippet enables a cron job in WordPress to automatically flush permalinks once per hour. Let’s break it down step by step to understand how it works.
In the first function, wpturbo_flush_permalinks_cron()
, we check if the wpturbo_flush_permalinks_event
scheduled event is already set up. If not, we schedule the event to run hourly using the wp_schedule_event()
function. The first argument, time()
, sets the initial time of the event to the current time. The second argument, 'hourly'
, indicates that the event should be triggered every hour. Finally, the third argument, 'wpturbo_flush_permalinks_event'
, specifies the name of the event to be scheduled.
if ( ! wp_next_scheduled( 'wpturbo_flush_permalinks_event' ) ) {
wp_schedule_event( time(), 'hourly', 'wpturbo_flush_permalinks_event' );
}
Next, we add an action hook 'wp'
to execute the wpturbo_flush_permalinks_cron()
function. The 'wp'
hook triggers at the beginning of every WordPress request. By attaching our function to this hook, we ensure that the cron job is properly initialized.
add_action( 'wp', 'wpturbo_flush_permalinks_cron' );
In the second function, wpturbo_flush_permalinks()
, we define the action to be performed when the wpturbo_flush_permalinks_event
occurs. In this case, it calls the flush_rewrite_rules()
function. This function flushes and regenerates WordPress’ rewrite rules, including permalinks, custom post types, and rewrite rules defined by plugins.
flush_rewrite_rules();
Finally, we add yet another action hook, 'wpturbo_flush_permalinks_event'
, to which we attach our wpturbo_flush_permalinks()
function using add_action()
. This ensures that whenever the scheduled event occurs, the flush_rewrite_rules()
function will execute.
add_action( 'wpturbo_flush_permalinks_event', 'wpturbo_flush_permalinks' );
By combining these code snippets, we can set up a cron job that flushes the permalinks in WordPress automatically once per hour. This is useful in scenarios where changes to rewrite rules or permalinks frequently occur, such as when developing or updating a plugin or theme.