Skip to content

Instantly share code, notes, and snippets.

@imath
Last active April 1, 2021 16:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imath/d1d6640b010ee83c16f39a1eb50c0f74 to your computer and use it in GitHub Desktop.
Save imath/d1d6640b010ee83c16f39a1eb50c0f74 to your computer and use it in GitHub Desktop.
Code snippet to let users chose their BuddyPress member type during the registration process
<?php
// This PHP tag is there to get the Gist formatting, make sure to only use one at the top or your script!
/**
* You can alternatively use the Users > Member type WP Admin screen
* to define your member types.
*/
function bettina_register_member_types() {
/**
* For more information about using code to register member types:
* @see https://codex.buddypress.org/developer/member-types/
*/
$member_types = array(
'newbies' => array(
'labels' => array(
'name' => __( 'Newbies', 'bettina' ),
'singular_name' => __( 'Newbie', 'bettina' ),
),
'has_directory' => 'newbies',
),
'intermediates' => array(
'labels' => array(
'name' => __( 'Intermediates', 'bettina' ),
'singular_name' => __( 'Intermediate', 'bettina' ),
),
'has_directory' => 'intermediates',
),
'experts' => array(
'labels' => array(
'name' => __( 'Experts', 'bettina' ),
'singular_name' => __( 'Expert', 'bettina' ),
),
'has_directory' => 'experts',
),
);
foreach ( $member_types as $key_type => $args_type ) {
bp_register_member_type( $key_type, $args_type );
}
}
add_action( 'bp_register_member_types', 'bettina_register_member_types' );
<?php
// This PHP tag is there to get the Gist formatting, make sure to only use one at the top or your script!
/**
* Inserts the HTML output under the last xProfile signup field.
*/
function bettina_account_member_type_field() {
// Only include the member types field if some member types are actually defined.
if ( ! bp_get_member_types() ) {
return;
}
$member_type_key = 'signup_member_type';
$submitted_member_type = '';
$existing_member_types = bp_get_member_types( array(), 'objects' );
if ( isset( $_POST[ $member_type_key ] ) ) {
$submitted_member_type = wp_unslash( $_POST[ $member_type_key ] );
}
?>
<div class="editfield">
<label for="<?php echo esc_attr( $member_type_key ); ?>"><?php esc_html_e( 'Choose the option that describes you the best', 'bettina' ); ?> <?php esc_html_e( '(required)', 'bettina' ); ?></label>
<?php do_action( 'bp_' . $member_type_key . '_errors' ); ?>
<select name="<?php echo esc_attr( $member_type_key ); ?>" id="<?php echo esc_attr( $member_type_key ); ?>">
<option value="">&mdash;</options>
<?php foreach ( $existing_member_types as $member_type ) : ?>
<option value="<?php echo esc_attr( $member_type->name ); ?>" <?php selected( $member_type->name, $submitted_member_type ); ?>><?php echo esc_html( $member_type->labels['singular_name'] ) ;?></option>
<?php endforeach; ?>
</select>
</div>
<?php
}
add_action( 'bp_signup_profile_fields', 'bettina_account_member_type_field' );
<?php
// This PHP tag is there to get the Gist formatting, make sure to only use one at the top or your script!
/**
* Validate the submitted value: a member type needs to be selected and exist.
*/
function bettina_signup_validate_member_type() {
$bp = buddypress();
$errors = new WP_Error();
$member_type_key = 'signup_member_type';
if ( ! isset( $_POST[ $member_type_key ] ) || ! $_POST[ $member_type_key ] ) {
$errors->add( 'empty_member_type', __( 'Please, we insist, do choose an option that describes you the best.', 'bettina' ) );
$bp->signup->errors[ $member_type_key ] = $errors->errors['empty_member_type'][0];
} else {
$submitted_member_type = wp_unslash( $_POST[ $member_type_key ] );
// Makes sure the member type exists.
if ( ! bp_get_member_type_object( $submitted_member_type ) ) {
$errors->add( 'unknown_member_type', __( 'Please, choose one of the options provided into the list.', 'bettina' ) );
$bp->signup->errors[ $member_type_key ] = $errors->errors['unknown_member_type'][0];
}
}
}
add_action( 'bp_signup_validate', 'bettina_signup_validate_member_type' );
<?php
// This PHP tag is there to get the Gist formatting, make sure to only use one at the top or your script!
/**
* Format the error message display for the BP Nouveau Template pack.
*
* NB: to use this filter you'll need to wait until BuddyPress 8.0.0 is released.
*
* @param string $error_message The HTML output for the error message.
* @param string $fieldname The name of the field the error is about.
* @return string The HTML output for the error message
* (which may have been formatted for BP Nouveau).
*/
function bettina_format_signup_error_message( $error_message = '', $fieldname = '' ) {
if ( 'signup_member_type' === $fieldname && 'nouveau' === bp_get_theme_compat_id() ) {
$bp = buddypress();
$error_message = nouveau_error_template( $bp->signup->errors['signup_member_type'] );
}
return $error_message;
}
add_filter( 'bp_members_signup_error_message', 'bettina_format_signup_error_message', 10, 2 );
<?php
// This PHP tag is there to get the Gist formatting, make sure to only use one at the top or your script!
/**
* Add the member_type to the signup metadata to make it availabale when the user will activate their account.
*
* @param array $signup_meta An array containing the signup metadata.
* @return array The array containing the signup metadata including the member type.
*/
function bettina_signup_append_member_type_meta( $signup_meta = array() ) {
if ( isset( $_POST['signup_member_type'] ) && $_POST['signup_member_type'] ) {
$submitted_member_type = wp_unslash( $_POST['signup_member_type'] );
// Makes sure the member type exists.
if ( bp_get_member_type_object( $submitted_member_type ) ) {
$signup_meta['member_type'] = $submitted_member_type;
}
}
return $signup_meta;
}
add_filter( 'bp_signup_usermeta', 'bettina_signup_append_member_type_meta', 10, 1 );
<?php
// This PHP tag is there to get the Gist formatting, make sure to only use one at the top or your script!
/**
* Assign the member type to the user who just activated their account.
*
* @param int $user_id The newly created user ID.
* @param string $key The activation key.
* @param array $signup The signup data.
*/
function bettina_signup_process_member_type( $user_id = 0, $key = '', $signup = array() ) {
if ( ! $user_id || ! $key ) {
return;
}
// Makes sure the member type exists.
if ( isset( $signup['meta']['member_type'] ) && bp_get_member_type_object( $signup['meta']['member_type'] ) ) {
bp_set_member_type( $user_id, $signup['meta']['member_type'] );
}
}
add_action( 'bp_core_activated_user', 'bettina_signup_process_member_type', 10, 3 );
<?php
/**
* BP Custom function file.
*
* Add this file into /wp-content/plugins/ to customize BuddyPress.
*
* @see https://codex.buddypress.org/themes/bp-custom-php/
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* You can alternatively use the Users > Member type WP Admin screen
* to define your member types.
*/
function bettina_register_member_types() {
/**
* For more information about using code to register member types:
* @see https://codex.buddypress.org/developer/member-types/
*/
$member_types = array(
'newbies' => array(
'labels' => array(
'name' => __( 'Newbies', 'bettina' ),
'singular_name' => __( 'Newbie', 'bettina' ),
),
'has_directory' => 'newbies',
),
'intermediates' => array(
'labels' => array(
'name' => __( 'Intermediates', 'bettina' ),
'singular_name' => __( 'Intermediate', 'bettina' ),
),
'has_directory' => 'intermediates',
),
'experts' => array(
'labels' => array(
'name' => __( 'Experts', 'bettina' ),
'singular_name' => __( 'Expert', 'bettina' ),
),
'has_directory' => 'experts',
),
);
foreach ( $member_types as $key_type => $args_type ) {
bp_register_member_type( $key_type, $args_type );
}
}
add_action( 'bp_register_member_types', 'bettina_register_member_types' );
/**
* Inserts the HTML output under the last xProfile signup field.
*/
function bettina_account_member_type_field() {
// Only include the member types field if some member types are actually defined.
if ( ! bp_get_member_types() ) {
return;
}
$member_type_key = 'signup_member_type';
$submitted_member_type = '';
$existing_member_types = bp_get_member_types( array(), 'objects' );
if ( isset( $_POST[ $member_type_key ] ) ) {
$submitted_member_type = wp_unslash( $_POST[ $member_type_key ] );
}
?>
<div class="editfield">
<label for="<?php echo esc_attr( $member_type_key ); ?>"><?php esc_html_e( 'Choose the option that describes you the best', 'bettina' ); ?> <?php esc_html_e( '(required)', 'bettina' ); ?></label>
<?php do_action( 'bp_' .$member_type_key . '_errors' ); ?>
<select name="<?php echo esc_attr( $member_type_key ); ?>" id="<?php echo esc_attr( $member_type_key ); ?>">
<option value="">&mdash;</options>
<?php foreach ( $existing_member_types as $member_type ) : ?>
<option value="<?php echo esc_attr( $member_type->name ); ?>" <?php selected( $member_type->name, $submitted_member_type ); ?>><?php echo esc_html( $member_type->labels['singular_name'] ) ;?></option>
<?php endforeach; ?>
</select>
</div>
<?php
}
add_action( 'bp_signup_profile_fields', 'bettina_account_member_type_field' );
/**
* Validate the submitted value: a member type needs to be selected and exist.
*/
function bettina_signup_validate_member_type() {
$bp = buddypress();
$errors = new WP_Error();
$member_type_key = 'signup_member_type';
if ( ! isset( $_POST[ $member_type_key ] ) || ! $_POST[ $member_type_key ] ) {
$errors->add( 'empty_member_type', __( 'Please, we insist, do choose an option that describes you the best.', 'bettina' ) );
$bp->signup->errors[ $member_type_key ] = $errors->errors['empty_member_type'][0];
} else {
$submitted_member_type = wp_unslash( $_POST[ $member_type_key ] );
// Makes sure the member type exists.
if ( ! bp_get_member_type_object( $submitted_member_type ) ) {
$errors->add( 'unknown_member_type', __( 'Please, choose one of the options provided into the list.', 'bettina' ) );
$bp->signup->errors[ $member_type_key ] = $errors->errors['unknown_member_type'][0];
}
}
}
add_action( 'bp_signup_validate', 'bettina_signup_validate_member_type' );
/**
* Format the error message display for the BP Nouveau Template pack.
*
* NB: to use this filter you'll need to wait until BuddyPress 8.0.0 is released.
*
* @param string $error_message The HTML output for the error message.
* @param string $fieldname The name of the field the error is about.
* @return string The HTML output for the error message
* (which may have been formatted for BP Nouveau).
*/
function bettina_format_signup_error_message( $error_message = '', $fieldname = '' ) {
if ( 'signup_member_type' === $fieldname && 'nouveau' === bp_get_theme_compat_id() ) {
$bp = buddypress();
$error_message = nouveau_error_template( $bp->signup->errors['signup_member_type'] );
}
return $error_message;
}
add_filter( 'bp_members_signup_error_message', 'bettina_format_signup_error_message', 10, 2 );
/**
* Add the member_type to the signup metadata to make it availabale when the user will activate their account.
*
* @param array $signup_meta An array containing the signup metadata.
* @return array The array containing the signup metadata including the member type.
*/
function bettina_signup_append_member_type_meta( $signup_meta = array() ) {
if ( isset( $_POST['signup_member_type'] ) && $_POST['signup_member_type'] ) {
$submitted_member_type = wp_unslash( $_POST['signup_member_type'] );
// Makes sure the member type exists.
if ( bp_get_member_type_object( $submitted_member_type ) ) {
$signup_meta['member_type'] = $submitted_member_type;
}
}
return $signup_meta;
}
add_filter( 'bp_signup_usermeta', 'bettina_signup_append_member_type_meta', 10, 1 );
/**
* Assign the member type to the user who just activated their account.
*
* @param int $user_id The newly created user ID.
* @param string $key The activation key.
* @param array $signup The signup data.
*/
function bettina_signup_process_member_type( $user_id = 0, $key = '', $signup = array() ) {
if ( ! $user_id || ! $key ) {
return;
}
// Makes sure the member type exists.
if ( isset( $signup['meta']['member_type'] ) && bp_get_member_type_object( $signup['meta']['member_type'] ) ) {
bp_set_member_type( $user_id, $signup['meta']['member_type'] );
}
}
add_action( 'bp_core_activated_user', 'bettina_signup_process_member_type', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment