Codex Home → Developer Resources → Add Meta Box to Admin Extended User Profile
Add Meta Box to Admin Extended User Profile
BuddyPress 2.0 allows admins to edit user profile fields from the Dashboard>>Users>>Edit User page. This extended profile page offers the ability to add your own settings for a user. This page gives a simple example of how to add the meta boxes to the extended profile page. What you add to the meta box can be a myriad of options. NOTE: any meta boxes you add are only editable by an admin. The info does not show on the front end user profile. You can put this example code in your bp-custom.php file.
This function adds our meta box to the the user extended profile page in the admin.
function bp_user_meta_box() {
add_meta_box(
'metabox_id',
__( 'Metabox Title', 'buddypress' ),
'bp_user_inner_meta_box', // function that displays the contents of the meta box
get_current_screen()->id
);
}
add_action( 'bp_members_admin_user_metaboxes', 'bp_user_meta_box' );
This function outputs the content of the meta box. The skies the limit here. You can add almost any information to want. You could have a meta box that you enter notes about that user or show information from another plugin.
function bp_user_inner_meta_box() {
?>
This is where you write your form inputs for user settings. Or you can output information pertaining to this user. For example, the Achievements plugin could show the users badges here.
<?php
}
This function saves any form inputs you might include in the meta box.
function bp_user_save_metabox() {
if( isset( $_POST['save'] ) ) {
$user_id = isset( $_GET['user_id'] ) ? $_GET['user_id'] : 0;
// you will need to use a $_POST param and validate before saving
$meta_val = isset( $_POST['form_value'] ) ? sanitize_text_field( $_POST['form_value'] ) : '';
// the $meta_val would be a $_POST param from inner meta box form
update_user_meta( $user_id, 'user_meta_key', $meta_val );
}
}
add_action( 'bp_members_admin_update_user', 'bp_user_save_metabox' );