BuddyPress Xprofile fields to display on "wordpress user edit admin panel" - wordpress

I am building a buddypress/bbpress, website with xprofile registration fields. I am using New User Approve plugin to approve each users manually since we need to check information of each user before they get activated.
Problem here is, I am unable to check/view the xprofile field values in Wordpress user edit admin panel. All I have is the username change password, changing roles etc. I want the admin panel to display the extra information of the registered user so that I can check the info and approve . Anyone can help me out to solve this problem.

Might be similar to this.. haven't tried the code though....
Replace the key value 'xprofile_key_birthday' with actual xprofile keys in Buddypress DB.
Note: This code only displays the values on the edit screen and doesn't insert or update anything.
<?php
add_action( 'show_user_profile', 'showmy_extra_profile_fields' );
add_action( 'edit_user_profile', 'showmy_extra_profile_fields' );
function showmy_extra_profile_fields( $user ) { ?>
<h3>Extra profile information</h3>
<table class="form-table">
<tr>
<th><label>xprofile_key_birthday</label></th>
<td>
<?php
if( function_exists( 'xprofile_get_field_data' ) ) {
$xprofile_value = xprofile_get_field_data('xprofile_key_birthday', $user->ID );
}
else {
$xprofile_value = '';
}
?>
<input type="text" name="xprofile_key_birthday" id="xprofile_key_birthday" value="<?php echo esc_attr( $xprofile_value ); ?>" class="regular-text" readonly />
</td>
</tr>
</table>
<?php
}
?>

I use the free Wangguard plugin on my BuddyPress/bbPress website. All I need to do is click on "Users" in the Wangguard menu on Wordpress dashboard sidebar and then click on "BP Profile" under the member username column. I can view and even edit the member profile from there. Hope it helps.

Related

How to create Unique User ID in WordPress

I need to add a users from WordPress backend. So, I want to generate unique User IDs for them. But I can achieve it. Can somebody help me out on this.
Here is what I cam so far. It is better if I could append the WordPress default generated unique user id to end of my field.
// Customize the user fields
function custom_user_profile_fields($user) {
$rand = randomGen(1000,5000,1);
$auth_Token = '2021-ABCD-'.$rand[0];
?>
<h3>Profile Information</h3>
<table class="form-table">
<tr>
<th><label for="srn">Student Registration Number</label></th>
<td>
<input type="text" class="regular-text" name="srn" value="<?php echo (get_option('srn') ? esc_attr( get_the_author_meta( 'srn', $user->ID ) ) : $auth_Token); ?>" id="srn" readonly /><br />
</td>
</tr>
</table>
<?php
}
add_action( 'show_user_profile', 'custom_user_profile_fields' );
add_action( 'edit_user_profile', 'custom_user_profile_fields' );
add_action( "user_new_form", "custom_user_profile_fields" );
function save_custom_user_profile_fields($user_id) {
# again do this only if you can
if(!current_user_can('manage_options'))
return false;
# save my custom field
update_usermeta($user_id, 'srn', $_POST['srn']);
}
add_action('user_register', 'save_custom_user_profile_fields');
add_action('profile_update', 'save_custom_user_profile_fields');
When a user added I can't get the previously saved Index Number to the field. It's generating another and showing again. As I am not an expert for this, could anybody help me out with this?
Thanks in advance
You were on the right path to solve the problem, but you made some mistakes.
You can use the working code below:
Creation of fields
add_action( 'user_new_form', 'bks_add_student_reg_number_field' );
add_action( 'edit_user_profile', 'bks_add_student_reg_number_field' );
add_action( 'show_user_profile', 'bks_add_student_reg_number_field' );
function bks_add_student_reg_number_field( $user ) {
?>
<h3><?php _e("Extra profile information", "blank"); ?></h3>
<table class="form-table">
<tr>
<th><label for="student_reg_number"><?php _e("Student Registration Number"); ?></label></th>
<td>
<?php if (is_object($user)) { ?>
<input type="text" name="student_reg_number" id="student_reg_number" value="<?php echo esc_attr( get_the_author_meta( 'student_reg_number', $user->ID )); ?>" class="regular-text" readonly disabled="disabled"/><br />
<?php } else { ?>
<input type="text" name="student_reg_number" id="student_reg_number" class="regular-text" /><br />
<span class="description"><?php _e("Please enter Student registration number."); ?></span>
<?php } ?>
</td>
</tr>
</table>
<?php }
This creates a field option at new user generation and edit form.
The field is only enabled when a new user is created. If the registered user has already have a value the value would be shown with no access to change it.
Save field value
add_action( 'user_register', 'bks_student_reg_number_field' );
function bks_student_reg_number_field($user_id) {
// You can maybe add checks here whch would determine if the users role is customer
// or not or maybe validate the number.
if(!current_user_can('manage_options'))
return false;
$reg_value = get_user_meta($user_id, 'student_reg_number', true);
if (isset( $_POST['student_reg_number']) && $_POST['student_reg_number'] !== $reg_value) {
update_user_meta($user_id, 'student_reg_number', $_POST['student_reg_number'].'-'.$user_id);
}
}
Only user_register hook is used to prevent updation on profile edit.
You can see the user_id appended with a - infront of it in the field and the field is readonly and disabled.
If you want user to be able to edit the id after registering as well then. do the following.
Add profile_update hook. add_action( 'profile_update', 'bks_student_reg_number_field' ); as well so that the code runs when you update the user as well.
Remove readonly and disabled attribute from the form.
The code is TESTED and WORKS.

Add exrtra user approve status filed in user profile, visible to admin only and send mail if checkbox is checked at first time in wordpress?

I want to give the functionality to admin to approve user status manually. I was able to give this functionality, but having some issue in this functionality. How can I hide this field from user's profile for user role. i.e. Field should be visible to admin only. If I hide this filed from user profile using jQuery then when user hits update profile button at that time value is updated. I want value to be same as per user selected (set by admin). I also want to send mail to user if checkbox is selected from uncheck to check, but currently it is sending mail every time. How can i solve out this issue?
/** Add user approve field */
add_action( 'user_new_form', 'Add_user_fields' );
add_action( 'edit_user_profile', 'Add_user_fields' );
add_action( 'show_user_profile', 'Add_user_fields' );
function Add_user_fields( $user ) { ?>
<h2 class="user_prmission">User Permission</h3>
<table class="form-table">
<tr class="user-approve_status-wrap">
<th><label for="dropdown">Approve User Permission</label></th>
<td>
<?php
//get dropdown saved value
$checked = (isset($user->artwork_approved) && $user->artwork_approved) ? ' checked="checked"' : '';
?>
<label for="artwork_approved">
<input name="artwork_approved" type="checkbox" id="artwork_approved" value="1" <?php echo $checked; ?>>
<?php _e('Approve Status','AA'); ?>
</label>
</td>
</tr>
</table>
<?php
}
/* Update selected option **/
add_action( 'user_register', 'save_user_fields');
add_action( 'personal_options_update', 'save_user_fields' );
add_action( 'edit_user_profile_update', 'save_user_fields' );
function save_user_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) )
return false;
update_user_meta($user_id, 'artwork_approved', isset($_POST['artwork_approved']));
/* Send email to agent to notify about thier account is approved. **/
if (get_user_meta($user_id, 'artwork_approved', true)) {
// sent mail if checkbox is selected from unchecked to checked,and hide this filed from user profile only visible to admin.
}
}
What I understand from your question is, you have added a custom field on user profile which is a checkbox. You want that field to be editable only by admins and not by the users.
If this understanding is correct, you can check the user capabilities of current user while rendering the custom field on user profile page.
That way, if current user has administrator privileges only then the custom field will be rendered. If a non admin user is logged in then the custom field will not be rendered.
Also, in your function to add custom field, you are setting the value of checkbox to '1' irrespective of whether it is checked or not. I guess that is causing the email to be sent every time. You should set the value of checkbox based on your condition.
So your function to add custom field will become as follows.
/** Add user approve field */
add_action( 'user_new_form', 'Add_user_fields' );
add_action( 'edit_user_profile', 'Add_user_fields' );
add_action( 'show_user_profile', 'Add_user_fields' );
function Add_user_fields( $user ) { ?>
if( !current_user_can( 'manage_options' ) ){
return;
}
<h2 class="user_prmission">User Permission</h3>
<table class="form-table">
<tr class="user-approve_status-wrap">
<th><label for="dropdown">Approve User Permission</label></th>
<td>
<?php
//get dropdown saved value
if( isset($user->artwork_approved) && $user->artwork_approved ){
$checked = 'checked="checked"';
$value = '1';
}else{
$checked = null;
$value = null;
}
?>
<label for="artwork_approved">
<input name="artwork_approved" type="checkbox" id="artwork_approved" value="<?php echo $value; ?>" <?php echo $checked; ?>>
<?php _e('Approve Status','AA'); ?>
</label>
</td>
</tr>
</table>
<?php
}
Hope this helps.

Wordpress Custom field doesn't update DB consistently

I'm creating a custom field on user-edit admin page. I added the custom code to functions.php.
Problem: Sometimes it updates the db and sometimes it doesn't update the db.
I thought it was caching but it's not that. Can someone tell me why it would be updating sometimes but not all the time? Do you see a problem with my code?
Code:
add_action( 'show_user_profile', 'be_show_extra_profile_fields' );
add_action( 'edit_user_profile', 'be_show_extra_profile_fields' );
function be_show_extra_profile_fields( $user ) { ?>
<table class="form-table">
<tr>
<th><label for="contact">Phone</label></th>
<td>
<input type="text" name="phone" id="phone" value="<?php echo esc_attr( get_the_author_meta( 'phone', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description">Enter phone number.</span>
</td>
</tr>
</table>
<?php }
add_action( 'personal_options_update', 'be_save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'be_save_extra_profile_fields' );
function be_save_extra_profile_fields( $user_id ) {
if ( ! current_user_can( 'edit_user', $user_id ) ) {
return false;
}
update_usermeta( $user_id, 'phone', esc_attr( $_POST['phone'] ) );
}
UPDATE
I now know why the admin form updates the db sometimes and sometimes it does not update the db. The admin form has a phone field plus on the profile page there's phone field also. When I updated the phone in admin and refreshed the page on the profile page it updated it when the textbox was not filled in. When the textbox was filled in and I updated the form in admin, refreshing the profile page would insert the phone number from the profile page. So there's an issue with the profile page, it inserts data on refresh.
I now know why the admin form updates the db sometimes and sometimes it does not update the db. The admin form has a phone field plus on the profile page there's phone field also. When I updated the phone in admin and refreshed the page on the profile page it updated it when the textbox was not filled in. When the textbox was filled in and I updated the form in admin, refreshing the profile page would insert the phone number from the profile page. So there's an issue with the profile page, it inserts data on refresh. The admin form was updating the db. The profile form was making it seem it was not updating because when refreshed it would update the db with that value.

How would I add a user registration detail to wp_users?

Here is my predicament right now, I currently run a Minecraft Server as alot of people in my community like to play it. The way I currently have it set up, users who sign up on the website have access to the Minecraft server. Minecraft connects to the database and checks to see if they have registered or if their name is there at all. But the problem with this is that users have to sign up with their Minecraft username, some user;s do not like to do that. Right now I would just like a column in wp_users to display a customizable text field saying Minecraft username.
Sorry if this is a duplicate at all.
Thank you for the help.
Instead of creating a new column in the wp_users table which I would advise against, you can use user metadata. You can update and get usermeta.
If you are wanting to code this yourself I think you will need to create your own signup form and the accompanying PHP file to handle it as I don't think there are the correct hooks to get everything you need, although I could be wrong. I'm happy to run you through this if required.
Otherwise, you could try some plugins, although I've not done this. Perhaps this or this - Note I've not tried either of these and not 100% sure they will do what you are after.
To save and show user meta fields on the users profile, try this in your functions.php file;
<?php
add_action( 'show_user_profile', 'add_user_meta_fields' );
add_action( 'edit_user_profile', 'add_user_meta_fields' );
add_action( 'personal_options_update', 'update_user_meta_fields' );
add_action( 'edit_user_profile_update', 'update_user_meta_fields' );
function update_user_meta_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) ) { return false; }
update_user_meta( $user_id, 'mincraftUser', $_POST['mincraftUser'] );
}
function add_user_meta_fields( $user ) { ?>
<h3>Extra Custom Meta Fields</h3>
<table class="form-table">
<tr>
<th><label for="mincraftUser">Twitter User Name</label></th>
<td>
<input type="text" id="mincraftUser" name="mincraftUser" size="20" value="<?php echo esc_attr( get_the_author_meta( 'mincraftUser', $user->ID )); ?>">
<span class="description">Please enter your Twitter Account User name, eg: oneTarek</span>
</td>
</tr>
</table>
<?php }

Wordpress to display custom USERMETA field on User Profile Dashboard?

I have added and been using a USERMETA field (called: user_custom_hash) from the wp_usermeta table. Means i've been Insert/Edit/Deleting the field from the code somewhere.
No needed to display it out before.
So now ..
how to DISPLAY this USERMETA field on the User Profile page of Wordpress Admin Dashboard? (I mean the "Edit User" page on the Admin Dashboard.)
For example:
Custom Hash Key: __239cc68ba2423ddf67dcfcd4aa2fa83b__
I mean, to display out at somewhere here:
Note:
Wordpress Version currently: 3.5.0
It's ok now as i got it by myself, like this:
add_action( 'show_user_profile', 'display_user_custom_hash' );
add_action( 'edit_user_profile', 'display_user_custom_hash' );
function display_user_custom_hash( $user ) { ?>
<h3>USERMETA Fields</h3>
<table class="form-table">
<tr>
<th><label>Custom Hash Key</label></th>
<td><input type="text" value="<?php echo get_user_meta( $user->ID, 'user_custom_hash', true ); ?>" class="regular-text" readonly=readonly /></td>
</tr>
</table>
<?php
}
Add this snippet at the bottom of functions.php file of my current Theme folder.
Note:After that entire code-above should be then, just the normal ?> for the entire file as normally as before.

Resources