¿Estás buscando una manera de limpiar los permalinks una vez cada hora? Si bien es probable que haya un plugin para esto, hemos creado un fragmento de código rápido que se puede utilizar para limpiar permalinks una vez por hora cron job en WordPress.
Para confirmar que los permalinks están siendo vaciados, simplemente añade un comentario justo antes de # END WordPress en el archivo .htaccess. Una vez que los enlaces permanentes se vacíen, el comentario desaparecerá.
Instrucciones:
Todo lo que tienes que hacer es añadir este código al archivo functions.php de tu tema o en un plugin específico del sitio:
// 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(); }
Nota: Si es la primera vez que añades fragmentos de código en WordPress, consulta nuestra guía sobre cómo añadir correctamente fragmentos de código en WordPress, para no romper accidentalmente tu sitio.
Si te ha gustado este fragmento de código, por favor, considere revisar nuestros otros artículos en el sitio como: 15 mejores plugins de marketing de contenidos y cómo crear un formulario de donación en WordPress.
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/`
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();
}
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.
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?
It’s possible that the file is not being overwritten by the snippet. You may want to contact the host about this.
Thanks, man!
But, what if I want to do it every 10 minutes?
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.
Sadly, you may want to have a developer look into this.