Howdy! Recently, I have been working a lot with WordPress engine, modyfying behind the site by writing plugins. I wonder to share my knowledge I collected under The Wordrpess Optimalizations tag, where a lot of simple tricks will be published. I also know that in the web there are a few of advises related with turning WordPress optimized and combination with your’s flair, the WordPress can become a powerfull, huge toll able to build simply sites as well as advanced solutions. The main advantage of WordPress is well developed backend, so you don’t have to care about beautiful and simply way to publish content and concentrate in frontent, striving to make a website much friendly to end user.
Unfortunately, the WordPress has many features that in common are not required in your project. Obviously, it generates relatively huge overhead to server, database and so one.
This post describes how to disable the „Trash” feature, which has been introduced in version 2.9 aimed to keep posts until permament deletion, such as deleting files in operating system. Deleted items are not deleted permanently from the database in fact, instead they appears in „Trash” by marking them as deleted, setting the flag.
Disabling trash manually
To disable trash in wordpress, you can simply define the constant EMPTY_TRASH_DAYS
, for example in your wp-config.php
.
define('EMPTY_TRASH_DAYS', 0);
From now, all options „Move to trash” will no longer apperar in admin pages, the functionality is just disabled. Just… but copies of posts marked as deleted are still in the database. To truncate them, just execute the sql statement:
DELETE p, p_rel, p_meta
FROM wp_posts p
LEFT JOIN wp_term_relationships p_rel ON (p.ID = p_rel.object_id)
LEFT JOIN wp_postmeta p_meta ON (p.ID = p_meta.post_id)
WHERE p.post_status = 'trash'
All posts marked as „Move to trash” will be deleted, and theirs post meta (custom fields) and taxonomy relations will be truncated too. The database now is clean.
Writing simple plugin.
We will write a simple plugin. In the /wp-content/plugins/MyPlugin/
create a file plugin.php
, and the code within:
define('EMPTY_TRASH_DAYS', 0);
register_activation_hook(__FILE__, function() {
global $wpdb;
$query = "DELETE p, p_rel, p_meta
FROM " . $wpdb->posts . " p
LEFT JOIN " . $wpdb->term_relationships . " p_rel ON (p.ID = p_rel.object_id)
LEFT JOIN " . $wpdb->postmeta . " p_meta ON (p.ID = p_meta.post_id)
WHERE p.post_status = 'trash'";
$wpdb->query($query);
});
The Trash feature is disabled by setting the EMPTY_TRASH_DAYS
constant, and posts moved to trash will be deleted while plugin activation. We used register_activation_hook function with anonymous function javascript-like callback function (PHP 5.3).
Download the plugin
The plugin compiled to be ready to install is available here:
Hope the post will be helpfull.