Skip to content

Instantly share code, notes, and snippets.

@imath
Created December 16, 2012 18:11
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save imath/4310524 to your computer and use it in GitHub Desktop.
Save imath/4310524 to your computer and use it in GitHub Desktop.
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.
<?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 );
}
}
@FranciscoHV
Copy link

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment