Run a function when a new site is created in WordPress Multisite

I am currently developing a multi-site plug-in for WordPress. I need to run a function when a new site is created.

Related posts

Leave a Reply

2 comments

  1. I guess you are looking for an action/filter.

    wpmu_new_blog this action is fired in wpmu_create_blog function (wp-includes/ms-functions.php)

    do_action( 'wpmu_new_blog', $blog_id, $user_id, $domain, $path, $site_id, $meta );
    
  2. Since WP 5.1.0, wpmu_new_blog has been deprecated in favour of wp_insert_site:

    /**
     * Fires once a site has been inserted into the database.
     *
     * @since 5.1.0
     *
     * @param WP_Site $new_site New site object.
     */
    do_action( 'wp_insert_site', $new_site );
    

    Use it like this in your plugin or theme:

    add_action('wp_insert_site', function($site) {
      // do something
    });
    

    A good place to look for available hooks is wp-includes/ms-default-filters.php

    wp_insert_site on WordPress Developer Handbook