Search code examples
phpwordpressfunctionmultisite

WordPress Multisite - (de)activate site action


I'm searching the WordPress codex for this but I can't seem to find any article on this issue.

When you have installed WordPress multisite and login as the superadmin you have the ability to archive,(de)activate and delete sites. This is however from the 'network' part of the installation, that only the superadmin can see. I want to place take the (de)activate option in every site's dashboard as well, so a specific (non (super)admin) role can use them to.

I want to know, is there a function I can use to display the (de)activate (depending on if the sites is active/deactive at that moment) links where I want to?

If there is not, where is the information about whether a site is active or not stored ? I was hoping for an option with a boolean in it but I can't seem to find it. This way I will be able to check myself if a site is active or not and depending on that display the correct link?

Sorry if my question is unclear or confusing.

Thanks in advance!


Solution

  • For the specific username rights I used:

    //give the right to deactivate sites to 'username' if the user doesn't have it already
    $user = new WP_User( 'username' );
    if ( ! $user->has_prop( 'can_manage_sites' ) ) {
       $user->add_cap( 'can_manage_sites' );
    }  
    if ( ! $user->has_prop( 'username' ) ) {
       $user->add_cap( 'manage_sites' );
    } 
    
    //ofcourse replace 'username' with the username you need
    

    In my view (wich loops mu sites) I simply used an if statement to check if the site was active or not to know what link to use (activate/deactivate)

    //deactivated, show activate link
    if(get_blog_status( $site->blog_id, 'deleted' ) == '1'){
        echo '<span class="dashicons dashicons-no" style="color:red;"></span> Niet actief <br/>'; 
        echo '<a href="' . esc_url( wp_nonce_url( network_admin_url( 'sites.php?action=confirm&amp;action2=activateblog&amp;id=' . $site->blog_id ), 'activateblog_' . $site->blog_id ) ) . '">' . __( 'Activate' ) . '</a>';
    }
    //activated, show deactivate link
    else {
        echo '<span class="dashicons dashicons-yes" style="color: green;"></span> Actief<br/>';
        echo '<a style="color:red;" href="' . esc_url( wp_nonce_url( network_admin_url( 'sites.php?action=confirm&amp;action2=deactivateblog&amp;id=' . $site->blog_id ), 'deactivateblog_' . $site->blog_id ) ) . '">' . __( 'Deactivate' ) . '</a>';
    }
    

    This code will get you the link you need to (de)activate the site wich will get you to a confirmationpage.In this confirmationpage the user can see a 'sites'tab in the menu, when they click it they have the options for all the sites in the mu. This didn't matter for me since the people who will be using this functionality are colleages and know they shouldn't go there. But you might want to hide that if it concerns people who shouldn't be able to see that