BuddyDev

Search

[Resolved] BuddyPress Registration Control

  • Participant
    Level: Guru
    Posts: 900
    Tosin on #47833

    Hello Brajesh,

    Please can you take a look at this code I cant seem to add the settings option in the default buddypress options page

     /*
     * Plugin Name: BuddyPress Registration Control
     * Description: Allows administrators to enable or disable user registration and select a URL to redirect to when registration is disabled.
     * Version: 1.0
     */
    
    // Register the new setting in the BuddyPress options group
    function custom_bp_register_settings() {
    	register_setting( 'bp', 'bp-disable-site-registration', 'sanitize_text_field' );
    	register_setting( 'bp', 'bp-disable-site-registration-redirection-url', 'sanitize_text_field' );
    }
    add_action( 'admin_init', 'custom_bp_register_settings' );
    
    // Add the new setting to the BuddyPress options page
    function custom_bp_add_settings_field() {
    	add_settings_field(
    		'bp-disable-site-registration',
    		__( 'Registration', 'buddypress' ),
    		'custom_bp_settings_field_html',
    		'buddypress',
    		'bp_settings_tab_general',
    		array(
    			'label_for' => 'bp-disable-site-registration',
    		)
    	);
    	add_settings_field(
    	'bp-disable-site-registration-redirection-url',
    		__( 'Redirection URL', 'buddypress' ),
    		'custom_bp_settings_redirection_url_html',
    		'buddypress',
    		'bp_settings_tab_general',
    		array(
    			'label_for' => 'bp-disable-site-registration-redirection-url',
    		)
    	);
    }
    add_action( 'bp_admin_settings_sanitize_option', 'custom_bp_add_settings_field', 10, 1 );
    
    // Output the HTML for the setting field
    function custom_bp_settings_field_html() {
    	$value = get_option( 'bp-disable-site-registration', 'open' );
    	?>
    	<select name="bp-disable-site-registration" id="bp-disable-site-registration">
    		<option value="open" <?php selected( $value, 'open' ); ?>><?php _e( 'Open', 'buddypress' ); ?></option>
    		<option value="closed" <?php selected( $value, 'closed' ); ?>><?php _e( 'Closed', 'buddypress' ); ?></option>
    	</select>
    	<?php
    }
    
    // Output the HTML for the redirection URL field
    function custom_bp_settings_redirection_url_html() {
    	$value = get_option( 'bp-disable-site-registration-redirection-url' );
    	?>
    	<input type="text" name="bp-disable-site-registration-redirection-url" id="bp-disable-site-registration-redirection-url" value="<?php echo esc_attr( $value ); ?>" />
    	<?php
    }
    
    // Redirect users if registration is closed
    function custom_bp_redirect_registration() {
    	if ( get_option( 'bp-disable-site-registration' ) === 'closed' ) {
    		wp_redirect( get_option( 'bp-disable-site-registration-redirection-url' ) );
    		exit;
    	}
    }
    add_action( 'bp_screens', 'custom_bp_redirect_registration' ); 

    Thanks

  • Participant
    Level: Guru
    Posts: 900
    Tosin on #47863

    What im trying to achieve is instead of using the default wordpress settings option to disable registrations (Anyone can register). I want to leave this default option enabled but disable buddypress registration using page redirection

    when user tries to access (sign-up) page redirect the user to (coming-soon) page

  • Keymaster
    (BuddyDev Team)
    Posts: 24706
    Brajesh Singh on #47876

    Hi Tosin,
    The problem with above code is the option group name and section name.

    Please find it updated here.

    
    <?php
    /*
    * Plugin Name: BuddyPress Registration Control
    * Description: Allows administrators to enable or disable user registration and select a URL to redirect to when registration is disabled.
    * Version: 1.0
    */
    
    // Register the new setting in the BuddyPress options group
    function custom_bp_register_settings() {
    	register_setting( 'buddypress', 'bp-disable-site-registration', 'sanitize_text_field' );
    	register_setting( 'buddypress', 'bp-disable-site-registration-redirection-url', 'sanitize_text_field' );
    }
    add_action( 'bp_register_admin_settings', 'custom_bp_register_settings' );
    
    // Add the new setting to the BuddyPress options page
    function custom_bp_add_settings_field() {
    	add_settings_field(
    		'bp-disable-site-registration',
    		__( 'Registration', 'buddypress' ),
    		'custom_bp_settings_field_html',
    		'buddypress',
    		'bp_main',
    		array(
    			'label_for' => 'bp-disable-site-registration',
    		)
    	);
    	add_settings_field(
    		'bp-disable-site-registration-redirection-url',
    		__( 'Redirection URL', 'buddypress' ),
    		'custom_bp_settings_redirection_url_html',
    		'buddypress',
    		'bp_main',
    		array(
    			'label_for' => 'bp-disable-site-registration-redirection-url',
    		)
    	);
    }
    add_action( 'bp_register_admin_settings', 'custom_bp_add_settings_field', 10, 1 );
    
    // Output the HTML for the setting field
    function custom_bp_settings_field_html() {
    	$value = get_option( 'bp-disable-site-registration', 'open' );
    	?>
    	<select name="bp-disable-site-registration" id="bp-disable-site-registration">
    		<option value="open" <?php selected( $value, 'open' ); ?>><?php _e( 'Open', 'buddypress' ); ?></option>
    		<option value="closed" <?php selected( $value, 'closed' ); ?>><?php _e( 'Closed', 'buddypress' ); ?></option>
    	</select>
    	<?php
    }
    
    // Output the HTML for the redirection URL field
    function custom_bp_settings_redirection_url_html() {
    	$value = get_option( 'bp-disable-site-registration-redirection-url' );
    	?>
    	<input type="text" name="bp-disable-site-registration-redirection-url" id="bp-disable-site-registration-redirection-url" value="<?php echo esc_attr( $value ); ?>" />
    	<?php
    }
    
    // Redirect users if registration is closed
    function custom_bp_redirect_registration() {
    	if ( get_option( 'bp-disable-site-registration' ) === 'closed' ) {
    		wp_redirect( get_option( 'bp-disable-site-registration-redirection-url' ) );
    		exit;
    	}
    }
    add_action( 'bp_screens', 'custom_bp_redirect_registration' );
    
    

    I did not go through other sections of your code. In case you need assistance there, I can look into that.

    Regards
    Brajesh

  • Participant
    Level: Guru
    Posts: 900
    Tosin on #47904

    Hi Brajesh

    The code worked, it has been further improved to limit based of membership count and work only on bp_is_register_page() can you please review it

     <?php
    /*
    * Plugin Name: BuddyPress Registration Control
    * Description: Allows administrators to enable or disable user registration and select a URL to redirect to when registration is disabled.
    * Version: 1.0
    */
    
    // Register the new setting in the BuddyPress options group
    function custom_bp_register_settings() {
        register_setting( 'buddypress', 'bp-disable-site-registration', 'sanitize_text_field' );
        register_setting( 'buddypress', 'bp-disable-site-registration-redirection-url', 'sanitize_text_field' );
        register_setting( 'buddypress', 'bp-close-registration-at-users', 'intval' ); // added this line
    }
    add_action( 'bp_register_admin_settings', 'custom_bp_register_settings' );
    
    // Add the new setting to the BuddyPress options page
    function custom_bp_add_settings_field() {
        add_settings_field(
            'bp-disable-site-registration',
            __( 'Registration', 'buddypress' ),
            'custom_bp_settings_field_html',
            'buddypress',
            'bp_main',
            array(
                'label_for' => 'bp-disable-site-registration',
            )
        );
        add_settings_field(
            'bp-disable-site-registration-redirection-url',
            __( 'Redirection URL', 'buddypress' ),
            'custom_bp_settings_redirection_url_html',
            'buddypress',
            'bp_main',
            array(
                'label_for' => 'bp-disable-site-registration-redirection-url',
            )
        );
        add_settings_field( // added this block
            'bp-close-registration-at-users',
            __( 'Limit User Registration Count', 'buddypress' ),
            'custom_bp_close_registration_at_users_html',
            'buddypress',
            'bp_main',
            array(
                'label_for' => 'bp-close-registration-at-users',
            )
        );
    }
    add_action( 'bp_register_admin_settings', 'custom_bp_add_settings_field', 10, 1 );
    
    // Output the HTML for the setting field
    function custom_bp_settings_field_html() {
        $value = get_option( 'bp-disable-site-registration', 'open' );
        ?>
        <select name="bp-disable-site-registration" id="bp-disable-site-registration">
            <option value="open" <?php selected( $value, 'open' ); ?>><?php _e( 'Open', 'buddypress' ); ?></option>
            <option value="closed" <?php selected( $value, 'closed' ); ?>><?php _e( 'Closed', 'buddypress' ); ?></option>
        </select>
        <?php
    }
    
    // Output the HTML for the redirection URL field
    function custom_bp_settings_redirection_url_html() {
        $value = get_option( 'bp-disable-site-registration-redirection-url' );
        ?>
    	<input type="text" name="bp-disable-site-registration-redirection-url" id="bp-disable-site-registration-redirection-url" value="<?php echo esc_attr( $value ); ?>" />
        <?php
    }
    
    // Output the HTML for the close registration at users field // added this function
    function custom_bp_close_registration_at_users_html() {
        $value = get_option( 'bp-close-registration-at-users' );
        ?>
        <input type="number" name="bp-close-registration-at-users" id="bp-close-registration-at-users" value="<?php echo esc_attr( $value ); ?>" />
        <p class="description"><?php _e( 'Close registration when the total number of users reaches this number.', 'buddypress' ); ?></p>
        <?php
    }
    
    // Redirect users if registration is closed
    function custom_bp_redirect_registration() {
        $total_users = count_users(); // added this line
        $total_users = $total_users['total_users']; // added this line
        if ( bp_is_register_page() && ( get_option( 'bp-disable-site-registration' ) === 'closed' || $total_users >= get_option( 'bp-close-registration-at-users' ) ) ) { // modified this line
            wp_redirect( get_option( 'bp-disable-site-registration-redirection-url' ) );
            exit;
        }
    }
    add_action( 'bp_screens', 'custom_bp_redirect_registration' ); 
  • Participant
    Level: Guru
    Posts: 900
    Tosin on #47905

    I still faced some issues when the plugin is activated and setting option set to (open) the sign-up page is not loading

  • Participant
    Level: Guru
    Posts: 900
    Tosin on #47924

    Hi Brajesh

    This is the improved code, there are no more issues

     <?php
    /*
    * Plugin Name: BuddyPress Registration Control
    * Description: Allows administrators to enable or disable user registration and select a URL to redirect to when registration is disabled.
    * Version: 1.0
    */
    
    // Register the new setting in the BuddyPress options group
    function custom_bp_register_settings() {
        if ( ! function_exists( 'bp_is_active' ) ) {
            return;
        }
        register_setting( 'buddypress', 'bp-disable-site-registration', 'custom_bp_sanitize_registration_status' );
        register_setting( 'buddypress', 'bp-disable-site-registration-redirection-url', 'custom_bp_sanitize_redirection_url' );
        register_setting( 'buddypress', 'bp-close-registration-at-users', 'custom_bp_sanitize_user_registration_count' );
    }
    add_action( 'admin_init', 'custom_bp_register_settings' );
    
    // add sanitization function
    function custom_bp_sanitize_registration_status( $status ) {
        $allowed_values = array( 'open', 'closed' );
        if ( ! in_array( $status, $allowed_values ) ) {
            $status = 'open';
        }
        return $status;
    }
    
    function custom_bp_sanitize_redirection_url( $url ) {
        return wp_kses_post( $url );
    }
    
    function custom_bp_sanitize_user_registration_count( $count ) {
        return absint( $count );
    }
    
    // Add the new setting to the BuddyPress options page
    function custom_bp_add_settings_field() {
        if ( ! function_exists( 'bp_is_active' ) ) {
            return;
        }
            add_settings_field(
            'bp-disable-site-registration',
            __( 'Registration', 'buddypress' ),
            'custom_bp_settings_field_html',
            'buddypress',
            'bp_main',
            array(
                'label_for' => 'bp-disable-site-registration',
            )
        );
        add_settings_field(
            'bp-disable-site-registration-redirection-url',
            __( 'Redirection URL', 'buddypress' ),
            'custom_bp_settings_redirection_url_html',
            'buddypress',
            'bp_main',
            array(
                'label_for' => 'bp-disable-site-registration-redirection-url',
            )
        );
        add_settings_field( 
            'bp-close-registration-at-users',
            __( 'Limit User Registration Count', 'buddypress' ),
            'custom_bp_close_registration_at_users_html',
            'buddypress',
            'bp_main',
            array(
                'label_for' => 'bp-close-registration-at-users',
            )
        );
    }
    add_action( 'bp_register_admin_settings', 'custom_bp_add_settings_field', 10, 1 );
    
    // Output the HTML for the setting field
    function custom_bp_settings_field_html() {
        $value = get_option( 'bp-disable-site-registration', 'open' );
        ?>
        <select name="bp-disable-site-registration" id="bp-disable-site-registration">
            <option value="open" <?php selected( $value, 'open' ); ?>><?php _e( 'Open', 'buddypress' ); ?></option>
            <option value="closed" <?php selected( $value, 'closed' ); ?>><?php _e( 'Closed', 'buddypress' ); ?></option>
        </select>
        <?php
    }
    
    // Output the HTML for the redirection URL field
    function custom_bp_settings_redirection_url_html() {
        $value = get_option( 'bp-disable-site-registration-redirection-url', home_url() );
        ?>
        <input type="text" name="bp-disable-site-registration-redirection-url" id="bp-disable-site-registration-redirection-url" value="<?php echo esc_attr( $value ); ?>" required/>
        <?php
    }
    
    // Output the HTML for the close registration at users field
    function custom_bp_close_registration_at_users_html() {
        $value = get_option( 'bp-close-registration-at-users' );
        ?>
        <input type="number" name="bp-close-registration-at-users" id="bp-close-registration-at-users" value="<?php echo esc_attr( $value ); ?>" required/>
        <p class="description"><?php _e( 'Enter the maximum number of users allowed to register on the site. Leave it empty or set to 0 for no limit.', 'buddypress' ); ?></p>
        <?php
    }
    
    // Check if registration is open or closed
    function custom_bp_redirect_registration() {
        $total_users = count_users();
        $total_users = $total_users['total_users'];
        $registration_status = get_option( 'bp-disable-site-registration', 'open' );
        $limit_user_registration_count = get_option( 'bp-close-registration-at-users' );
        if ( bp_is_register_page() && $registration_status === 'closed' && ( (empty($limit_user_registration_count) || $limit_user_registration_count == 0) || $total_users >= $limit_user_registration_count ) ) { 
            wp_redirect( get_option( 'bp-disable-site-registration-redirection-url' ) );
            exit;
        }
    }
    add_action( 'bp_screens', 'custom_bp_redirect_registration' ); 
  • Keymaster
    (BuddyDev Team)
    Posts: 24706
    Brajesh Singh on #47953

    Hi Tosin,
    Code seems to be fine except for sanitizing url, wp_kses_post is overkill, esc_url_raw should be fine.

    Also, your redirection callback is very inefficient, hitting database for user count on each page load.

    Here is an updated version to avoid that.

    
    // Check if registration is open or closed
    function custom_bp_redirect_registration() {
    	if ( ! bp_is_register_page() ) {
    		return;
    	}
    
    	if ( get_option( 'bp-disable-site-registration', 'open' ) !== 'closed' ) {
    		return;
    	}
    
    	$total_users                   = count_users();
    	$total_users                   = $total_users['total_users'];
    	$limit_user_registration_count = absint( get_option( 'bp-close-registration-at-users' ) );
    
    	if ( $total_users >= $limit_user_registration_count ) {
    		wp_redirect( get_option( 'bp-disable-site-registration-redirection-url' ) ); // wp_safe_redirect?
    		exit;
    	}
    }
    
    

    Regards
    Brajesh

  • Participant
    Level: Guru
    Posts: 900
    Tosin on #47958
    This reply has been marked as private.
  • Keymaster
    (BuddyDev Team)
    Posts: 24706
    Brajesh Singh on #47993
    This reply has been marked as private.

You must be logged in to reply to this topic.

This topic is: resolved