X

How to Flush Permalinks Once Per Hour Cron Job

Snippets by IsItWP

Are you looking for a way to flush the permalinks once in every hour? While there’s probably a plugin for this, we have created a quick code snippet that you can use to flush permalinks once per hour cron job in WordPress.

To confirm permalinks are being flushed, simply add a comment just before # END WordPress in the .htaccess file. Once permalinks are flushed, the comment will disappear.

Instructions:

All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin:


// Flush permalinks every hour 
add_action('my_hourly_event', 'do_this_hourly');

function my_activation() {
	if ( !wp_next_scheduled( 'my_hourly_event' ) ) {
		wp_schedule_event(time(), 'hourly', 'my_hourly_event');
	}
}

add_action('wp', 'my_activation');

function do_this_hourly() {
	global $wp_rewrite;
	$wp_rewrite->flush_rules();
}

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly add code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our other articles on the site like: 15 best content marketing plugins and how to create a WordPress donation form.

Comments  Leave a Reply

  1. Thank you for this explanation! I was looking for a way to fix a problem in my website. Because, almost once a day the permalinks are broken, and I have to re-save them in order to get them working again. Have you had this problem? do you know what it might causing this?

    Thanks again, and I’ll test this solution on my website.

    1. Sadly, you may want to have a developer look into this.

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!
[63% OFF Today!]
[63% OFF Today!]
[60% OFF Today!]
[60% OFF Today!]
[50% OFF TODAY!]
[50% OFF TODAY!]
[63% OFF Today!]
[63% OFF Today!]
[60% OFF Today!]
[60% OFF Today!]
[50% OFF Today!]
[50% OFF Today!]