0

So I been tinkering with this thought. Working w/ WooCommerce Subscriptions - if the user is a recurring paid member, they can create and manage their own subsite.

However, say the recurring payments are over, than there user role from 'Administrator' will automatically be changed into 'Subscriber'.

Since there will only be one Administrator per subsite, and that person's role changes into a Subscriber, is there a way I can link:

if Subscriber, deactivate their site(s). else if Adminstrator, activate their site(s).

I would love to get some feedback on this!

Thanks! Roc.

1 Answer 1

1

I would start by looking into the set_user_role action hook. You would be passed 3 arguments to your callback function - the User ID, the new user role, and an array of the old user roles(that seems strange to me, that you can't pass an array of roles, but oh well).

So here's an example code - I'm not certain for how you would get the sites that the user owns, but it should give you a general idea:

function my_set_user_role_callback( $user_id, $new_role, $old_roles ) {
    // Get the user sites here - not sure how you'd do that, but there should be a way
    $user_sites = array();
    if ( $user_sites ) {
        if ( in_array( 'administrator', $old_roles ) && $new_role == 'subscriber' ) {
            foreach ( $user_sites as $site ) {
                // Archive the site
                $site->archived = true;
                update_blog_details( $site->userblog_id, (array) $site );
            }
        } elseif ( in_array( 'subscriber', $old_roles ) && $new_role == 'administrator' ) {
            foreach ( $user_sites as $site ) {
                // Un-archive the site
                $site->archived = false;
                update_blog_details( $site->userblog_id, (array) $site );
            }
        }
    }
}
add_action( 'set_user_role', 'my_set_user_role_callback', 10, 3 );

Once you figure-out how to get the user sites, fix the properties of the $site object with the proper ones(there's the chance that these would be correct, but still).


PS: You can put all of your code in a plugin and network activate it, or put it in the wp-content/mu-plugins/ directory so that it will always be enabled for all sites.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.