Discover in this tutorial how to prevent WordPress trash items from being automatically deleted by code.
WordPress has, along with the auto-save system, another that manages the items sent to the trash, like:
- Posts
- Pages
- Objects
- Comments
By default, WordPress removes items from the trash after 30 days.
You have already seen how to change its emptying frequency.
However, do you prefer not to delete anything from the WordPress trash until you decide?
No problem, you can also manage this easily.
Snippets to prevent the automatic emptying of the WordPress Trash
To prevent the wastebasket from emptying by itself, you have 2 options:
- Set a very high number of emptying days.
- Use a function to eliminate the task.
Here you have both:
1. Increase the number of days until the next emptying
Add the following fragment at the end of the wp-config .php file:
To see this and another 1097 code snippets of this website, login or subscribe here.
By setting such a high number of days, you prevent the deletion from occurring in a very long time.
2. Add a function that overrides the emptying task
To do this, enter the following code in functions.php:
To see this and another 1097 code snippets of this website, login or subscribe here.
In this way, you avoid the task of emptying the bin.
With both options, the items in the trash are automatically stopped from being deleted.
Do you want to get rid of them? Then you’ll have to use the “empty trash” option manually.
Conclusion
As you can see, with one line of code you can avoid that the elements in the WordPress trash are automatically removed.
If you have any question, please leave it in the comments. And if you want to give me a suggestion for future snippets, please send it through the contact form.
Benefits of being a subscriber. 🙂