public
Last active

This is a BuddyPress gist. Using it in your active theme will allow you to manage a regular WordPress user meta in the registration form. Just above the profile fields form, you'll see a new text input 'Usermeta'. You can customize the different references to a random user meta i used (bp_cr_usermeta) by your own.

  • Download Gist
bp-customize-registration.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
<?php
// Exit if accessed directly
if ( !defined( 'ABSPATH' ) ) exit;
 
 
/****
 
You should paste this into a file (in the example below : bp-customize-registration.php )
in your active theme's folder, then depending if your theme is a child theme or not you should
use this code in your functions.php to include the trick
 
not a child theme
require( get_template_directory() . '/bp-customize-registration.php' );
 
child theme
require( get_stylesheet_directory() . '/bp-customize-registration.php' );
 
****/
 
add_action('wp_head', 'bp_cr_css_fix');
 
function bp_cr_css_fix() {
if ( !bp_is_current_component( 'register' ) )
return;
?>
<style>
.standard-form #bp-cr-usermeta-fields{
float: right;
width: 48%;
margin-bottom:1em;
}
</style>
<?php
}
 
 
add_action( 'bp_after_account_details_fields', 'bp_cr_usermeta_fields' );
 
function bp_cr_usermeta_fields() {
?>
<div class="register-section" id="bp-cr-usermeta-fields">
<h4>Usermeta Profile</h4>
<div class="editfield">
<label for="bp_cr_usermeta">Usermeta</label>
<input type="text" name="bp_cr_usermeta" id="bp_cr_usermeta" value="<?php bp_cr_usermeta_fields_edit_value(); ?>" />
<?php do_action('bp_cr_usermeta_fields');?>
</div>
</div>
<?php
}
 
/* if the user made something wrong in registration form, we'll print the posted value */
function bp_cr_usermeta_fields_edit_value() {
echo wp_kses( $_POST['bp_cr_usermeta'], array() );
}
 
/*
Here we're adding our regular usermeta to BuddyPress/WPMU $usermeta array
*/
add_filter( 'bp_signup_usermeta', 'bp_cr_signup_usermeta', 10, 1);
 
function bp_cr_signup_usermeta( $usermeta ) {
if( !empty( $_POST['bp_cr_usermeta']) ) {
// let's strip any tags..
$usermeta['bp_cr_usermeta'] = wp_kses( $_POST['bp_cr_usermeta'], array() );
}
return $usermeta;
}
 
add_action( 'bp_core_signup_user', 'bp_cr_signup_user', 10, 5);
 
function bp_cr_signup_user( $user_id, $user_login, $user_password, $user_email, $usermeta ) {
if( !is_multisite() ) {
/* if multisite we need to update the user meta.
For multisite config, we'll take care of this by playing with {$wpdb->base_prefix}signups
once the user has activated his account*/
// let's add a filter if someone wants to apply some sanitizing treatments..
$bp_cr_usermeta = apply_filters('bp_cr_signup_user_usermeta', $usermeta['bp_cr_usermeta'] );
// finally we're adding the usermeta
if( !empty( $bp_cr_usermeta ) )
update_user_meta( $user_id, 'bp_cr_usermeta', $bp_cr_usermeta );
}
}
 
add_action( 'bp_core_activated_user', 'bp_cr_activated_user', 10, 3 );
 
function bp_cr_activated_user( $user_id, $key, $user ) {
if( is_multisite() ) {
/* this time, we only take care of multisite configs
as on single site config, the usermeta is already stored */
// let's add a filter if someone wants to apply some sanitizing treatments..
$bp_cr_usermeta = apply_filters('bp_cr_signup_user_usermeta', $user['meta']['bp_cr_usermeta'] );
// finally we're adding the usermeta
if( !empty( $bp_cr_usermeta ) )
update_user_meta( $user_id, 'bp_cr_usermeta', $bp_cr_usermeta );
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.