• 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
    */
    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
    */
    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.