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.
Related
I was trying to add custom fields using the hooks:
add_action( 'show_user_profile', 'extra_user_profile_fields' );
add_action( 'edit_user_profile', 'extra_user_profile_fields' );
and it perfectly fine but it only shows upon editing of users and not upon creating new users. How can I add custom fields upon creation? Can I also specify to only show this field depending on the selected user role?
Edit
Also tried the suggestion:
do_action( 'user_new_form', 'extra_user_profile_fields' );
But it doesn't seem to work
Add your custom fields using follows- hook to show it in add user section.
do_action( 'user_new_form', 'add-new-user' );
And the code snippet are as follows -
// Add extra custom data
function extra_user_profile_fields(){
?>
<h3>Extra User Profile data</h3>
<table class="form-table">
<tr>
<th><label for="company">Company Name</label></th>
<td>
<input type="text" class="regular-text" name="company_name" value="" id="company_name" /><br />
<span class="description">Add your company name.</span>
</td>
</tr>
</table>
<?php
}
add_action( "user_new_form", "extra_user_profile_fields" );
// Used to save extra custom data
function save_extra_user_profile_fields($user_id){
if(isset($_POST['company_name']))
update_user_meta($user_id, '_user_company', $_POST['company_name']);
}
add_action('user_register', 'save_extra_user_profile_fields');
And you can show or hide this fields for selected user role using scripts.
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.
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 }
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.
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.