Title: Multisite new site inserted / deleted
Last modified: July 22, 2019

---

# Multisite new site inserted / deleted

 *  [withemes](https://wordpress.org/support/users/withemes/)
 * (@withemes)
 * [6 years, 12 months ago](https://wordpress.org/support/topic/multisite-new-site-inserted-deleted/)
 * Hi, thank you for a very good plugin.
 * I found that on multi-site environment the plugin doesn’t create new table when
   a new site inserted and doesn’t delete the table when the a site deleted. So 
   I added those code myself and it works pretty well.
 * Could you please check it? If it’s good, please add them into the next update:
 * In Post_Views_Counter::__construct
 *  `// check when new blog created and deleted
    add_action( ‘wp_insert_site’, array(
   $this, ‘wpmu_site_inserted’ ) ); add_action( ‘wp_delete_site’, array( $this, ‘
   wpmu_site_deleted’ ) );`
 * class Post_Views_Counter methods:
 *  `/**
    * delete the table when site deleted * [@withemes](https://wordpress.org/support/users/withemes/)*/
   function wpmu_site_deleted( $site ) {
 *  global $wpdb;
    $current_blog_id = $wpdb->blogid;
 *  $blog_id = $site->blog_id;
 *  $activated_blogs = get_site_option( ‘post_views_counter_activated_blogs’, false,
   false );
    if ( ! $activated_blogs ) $activated_blogs = [];
 *  // switch to the new blog for a while to run activate single
    switch_to_blog(
   $blog_id );
 *  // delete table from database
    $wpdb->query( ‘DROP TABLE IF EXISTS ‘ . $wpdb-
   >get_blog_prefix( $blog_id ) . ‘post_views’ ); $this->remove_cache_flush();
 *  // remove from options
    foreach ( $activated_blogs as $k => $id ) { if ( $id
   == $blog_id ) unset( $activated_blogs[ $k ] ); }
 *  // switch back to the current blog
    switch_to_blog( $current_blog_id );
 *  update_site_option( ‘post_views_counter_activated_blogs’, $activated_blogs, 
   array() );
 *  }
 *  /**
    * create new tables when a new blog created * [@withemes](https://wordpress.org/support/users/withemes/)*/
   function wpmu_site_inserted( $site ) {
 *  global $wpdb;
    $current_blog_id = $wpdb->blogid;
 *  $blog_id = $site->blog_id;
 *  $activated_blogs = get_site_option( ‘post_views_counter_activated_blogs’, false,
   false );
    if ( ! $activated_blogs ) $activated_blogs = [];
 *  // switch to the new blog for a while to run activate single
    switch_to_blog(
   $blog_id ); $this->activate_single(); $activated_blogs[] = (int) $blog_id;
 *  // switch back to the current blog
    switch_to_blog( $current_blog_id );
 *  update_site_option( ‘post_views_counter_activated_blogs’, $activated_blogs, 
   array() );
 *  }`
 * Kind Regards,
 * WiThemes

The topic ‘Multisite new site inserted / deleted’ is closed to new replies.

 * ![](https://ps.w.org/post-views-counter/assets/icon-256x256.png?rev=2973820)
 * [Post Views Counter](https://wordpress.org/plugins/post-views-counter/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/post-views-counter/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/post-views-counter/)
 * [Active Topics](https://wordpress.org/support/plugin/post-views-counter/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/post-views-counter/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/post-views-counter/reviews/)

 * 0 replies
 * 1 participant
 * Last reply from: [withemes](https://wordpress.org/support/users/withemes/)
 * Last activity: [6 years, 12 months ago](https://wordpress.org/support/topic/multisite-new-site-inserted-deleted/)
 * Status: not a support question