delete_blog and deleted_blog hooks are deprecated since WordPress 5.1.0

In this article I will show you the difference between these hooks and how to replace them in WordPress Multisite 5.1.0

While I was busy completely rebuilding my own website and at the same time working on a difficult WooCommerce project 🇨🇦, I received an email from one of the customers who purchased my multisite plugin. He said that since he had updated to WP 5.1.0 version, he’s receiving this notice:

Notice: delete_blog is deprecated since WordPress version 5.1.0
Notice: delete_blog is deprecated since version 5.1.0

And I decided why not to share the process of how I’ve fixed this issue publicly.

What is the Difference between Deactivated and Removed Sites in WordPress Multisite?

But before we dive into action hooks replacements, it is critical to understand a difference between these two things.

When you Deactivate a website within your multisite network, it won’t be removed from the database, if you Delete it, its database tables will be dropped.

Everything seems clear in quick actions here:

Row actions for sites within WordPress Multisite Network
“Multisite Posts” menu link is also a part of my plugin. If you turn it on in plugin settings, on that page all the posts/post_types from your network will be displayed there.

But if you open a website settings page, you will see a Deleted attribute, if you check it, the website will be deactivated, not removed.

Blog settings page within WordPress Multisite Network, you can set a Deleted attribute if you want to deactivate a website
“Index” tab is also a part of my Multisite plugin

So when a website either deactivated or deleted, both delete_blog and deleted_blog were fired before 5.1.0 version, the difference is just in the second parameter $drop, which is false for deactivated websites and true for deleted ones.

delete_blog

This action hook was fired before a website is deactivated/removed, as I said the difference between these two actions was in $drop parameter.

do_action( 'delete_blog', int $site_id, bool $drop )

Since WordPress 5.1.0 we can easily replace the above action hook with two other hooks, the first one, wp_uninitialize_site will be fired for deleted websites.

do_action( 'wp_uninitialize_site', WP_Site $old_site )

Example:

add_action( 'wp_uninitialize_site', function( $blog ) {
	// do_something
});

And wp_update_site hook – for deactivated websites.

do_action( 'wp_update_site', WP_Site $new_site, WP_Site $old_site )

Example:

add_action( 'wp_update_site', function( $new_blog, $old_blog ) {
	if( !$old_blog->deleted && $new_blog->deleted ) {
		// do_something 
	}
}, 20, 2 );

Never forget about the condition statement (line 2), because the acton hook will be fired for any website status changes and not only.

Please note, that the new hooks accept not a $blog_id parameter but a whole WP_Site object, you can get a blog ID from the object easily – $blog->blog_id.

deleted_blog

This hook was fired after a website is completely deleted from database. And we can replace it with wp_delete_site – for deleted websites and wp_update_site for deactivated websites.

do_action( 'wp_delete_site', WP_Site $old_site )

I’ve already described the second hook wp_update_site above, so that’s all.

Well, I do not think that this tutorial brings me a lot of traffic and all that stuff, but I hope it will be helpful 🙃

Related

Need some help with WordPress Multisite?

If you need some professional developer help, I will be happy to assist you.

Contact me Who I am?

Leave a comment

php js HTML CSS Code