Question

As a network admin in multisite configuration, I have the option to enable or disable the user registration \ site registration. is there any way i can enable the user registration on s single sub site , not on all sites.

In my WP setup, I have a main blog which is managed by me and I dont want user to register here. i have a another blog installed on sub directory dedicated for forum. I would like to enable user registration here, so that user can use the forum

Était-ce utile?

La solution

The multisite setup allows you to enable and disable user registration at the network level but if you see the database it store the value in the wp_options tables for each site.

So we can try the below and see if this work.

Use the below code in the functions.php file.

function wpse_enable_user_registration( $blog_id = 1 ) {
    switch_to_blog( $blog_id );
    // Fetching the present option
    $user_registration_option = get_option( 'users_can_register', 0 );

    if( '0' == $user_registration_option )
        $site_registration_option = update_option( 'users_can_register', 1 );

    restore_current_blog(); // Switches back to the original blog

    return $site_registration_option;
}

If the updation is successful, you will get true else false

Now you can use the function to enable any subsites in the MU setup by passing the sub sites id to the function in place of $blog_id

Autres conseils

Multisite adds a filter for get_option('users_can_register') calls in ms-functions.php users_can_register_signup_filter. This filter circumvents any blog-level settings you try to implement.

/**
 * Check whether users can self-register, based on Network settings.
 *
 * @since MU
 *
 * @return bool
 */
function users_can_register_signup_filter() {
    $registration = get_site_option('registration');
    if ( $registration == 'all' || $registration == 'user' )
        return true;

    return false;
}
add_filter('option_users_can_register', 'users_can_register_signup_filter');
Licencié sous: CC-BY-SA avec attribution
Non affilié à wordpress.stackexchange
scroll top