Perform action on WPMU blog deletion

Hi guys 🙂 I know there is the wpmu_new_blog action hook which enables us to perform an action when a new WPMU blog is created. Is there something similar to this which enables us to perform an action when a WPMU blog is deleted? Something which looks like this:

add_action('blog_deletion_hook', 'function_to_perform')

Related posts

Leave a Reply

3 comments

  1. Yes, inside /wp-admin/includes/ms.php there is the action hook delete_blog.

    This test prevents a blog deletion:

    add_action( 'delete_blog', 'prevent_blog_delete_wpse_82961', 10, 2 );
    
    /**
    * @param int $blog_id Blog ID
    * @param bool $drop True if blog's table should be dropped. Default is false.
    */
    function prevent_blog_delete_wpse_82961( $blog_id, $drop ) 
    {
        wp_die( 'aborting delete_blog' );
    }
    
  2. WordPress use since version 5.1 a new hook.

    do_action( 'wp_delete_site', $old_site );

    The var $old_site is the deleted site object.

    If you need a hook before the blog will delete, use the hook below.

     * Fires before a site should be deleted from the database.
     *
     * Plugins should amend the `$errors` object via its `WP_Error::add()` method. If any errors
     * are present, the site will not be deleted.
     *
     * @since 5.1.0
     *
     * @param WP_Error $errors   Error object to add validation errors to.
     * @param WP_Site  $old_site The site object to be deleted.
     */
    do_action( 'wp_validate_site_deletion', $errors, $old_site );
    

    You should use a hook from this two options because the hook delete_blog is deprecated.