How to Automatically Empty Trash in WordPress

Home » Snippets » How to Automatically Empty Trash in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Are you tired of manually emptying the trash in your WordPress site? Having to constantly go through the process of deleting unwanted posts, comments, and other items from your trash folder can be time-consuming and repetitive. Thankfully, there’s a solution that can save you from this mundane task – automating the emptying of the trash in WordPress. In this article, we will discuss how you can set up your WordPress site to automatically empty the trash, allowing you to focus on more important tasks and keeping your site clean and optimized.

					function wpturbo_empty_trash() {
    add_action( 'wp_footer', 'wp_turbo_trash_js' );
}

function wp_turbo_trash_js() {
    ?>
    <script>
        jQuery(document).ready(function($) {
            var emptyTrash = setInterval( function(){
                $.ajax({
                    url: ajaxurl,
                    type: "POST",
                    data: {
                        'action': 'wp_turbo_empty_trash',
                    },
                    success: function( response ) {
                        console.log(response);
                    }
                });
            }, 60000 ); // 1 minute
        });
    </script>
    <?php
}
add_action( 'admin_init', 'wpturbo_empty_trash' );

function wp_turbo_empty_trash() {
    if ( current_user_can( 'manage_options' ) ) {
        $args = array(
            'post_type'      => 'post',
            'post_status'    => 'trash',
            'posts_per_page' => -1,
        );
        $trash_posts = get_posts( $args );
        
        if ( $trash_posts ) {
            foreach ( $trash_posts as $post ) {
                wp_delete_post( $post->ID, true );
            }
        }
    }
}
add_action( 'wp_ajax_wp_turbo_empty_trash', 'wp_turbo_empty_trash' );
add_action( 'wp_ajax_nopriv_wp_turbo_empty_trash', 'wp_turbo_empty_trash' );
				

The code snippet provided is a function that automatically empties the trash in WordPress. It consists of two main parts: the JavaScript function wp_turbo_trash_js() and the PHP function wp_turbo_empty_trash().

Let's start by explaining the JavaScript function. This function is wrapped within <script> tags and is used to send an AJAX request to the server to empty the trash. It is executed when the document is ready, which ensures that the necessary jQuery library is loaded.

Inside the wp_turbo_trash_js() function, we use the setInterval method to create a recurring task that sends an AJAX request every 1 minute (60000 milliseconds). The AJAX request is made using jQuery's $.ajax() function. We specify the URL of the AJAX handler (ajaxurl) and set the action parameter to wp_turbo_empty_trash. This action will be used in the PHP function to handle the AJAX request.

When the AJAX request is successful, the response is logged to the console using console.log(). This can be helpful for debugging purposes, but it can also be removed in a production environment.

Moving on to the PHP function wp_turbo_empty_trash(), we start by checking if the current user has the capability to manage options. This ensures that only users with the necessary permissions can empty the trash.

Next, we define the arguments for the get_posts() function to retrieve all posts in the 'trash' status. The post_type parameter is set to 'post', but you can modify it to include other post types if needed. We set the posts_per_page parameter to -1, which means all posts will be retrieved.

If there are posts in the trash, we loop through each post using a foreach loop. Inside the loop, we use the wp_delete_post() function to permanently delete each post by passing its ID as an argument. The second parameter is set to true, which bypasses the trash and deletes the post permanently.

Finally, we hook the wp_turbo_empty_trash() function to the AJAX actions wp_ajax_wp_turbo_empty_trash and wp_ajax_nopriv_wp_turbo_empty_trash. This ensures that the function is called when the AJAX request with the action wp_turbo_empty_trash is made by logged-in users (wp_ajax_) or non-logged-in users (wp_ajax_nopriv_).

By combining these two functions and the necessary actions, you can automatically empty the trash in WordPress at regular intervals using AJAX.

Register an account to save your snippets or go Pro to get more features.