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. You could use WP Cli if your hosting supports it for this with a cron task:
    `/path/to/wp rewrite flush –path=/path/to/website/files/`

  2. For those who need to flush the permalinks only once a day, here’s a code snippet you can use:

    // Flush permalinks once a day
    add_action(‘my_daily_event’, ‘do_this_daily’);

    function my_activation() {
    if (!wp_next_scheduled(‘my_daily_event’)) {
    wp_schedule_event(strtotime(‘midnight’), ‘daily’, ‘my_daily_event’);
    }
    }

    add_action(‘wp’, ‘my_activation’);

    function do_this_daily() {
    flush_rewrite_rules();
    }

  3. Hello,

    This is really useful, thank you.

    I just wanted to understand this.
    When you use the hook named ‘wp’ which I believe doesn’t exist, it means that the function ‘my_activation’ should run when?
    I’m trying to understand at which point this function will be called.

  4. I have added your code snippet to my functions.php file, and I have also added a comment to my .htaccess file as follows:

    # TEST for periodic flush of rewrite rules
    # END WordPress

    However, my test comment does not disappear, and I am wondering if this code snippet works at all. Am I missing something?

    1. It’s possible that the file is not being overwritten by the snippet. You may want to contact the host about this.

  5. Thanks, man!

    But, what if I want to do it every 10 minutes?

  6. 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 Cancel reply

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!