Add custom fields upon user creation - wordpress

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.

Related

Adding custom fields to a woocommerce product vendor's 'edit vendor' page, that a vendor can also edit, that show up in their public profile?

I have been able to find a way to add a custom field to a Woocommerce Product Vendor's "Edit Vendor" screen, that the Admin (me) can edit - using the following code. But, the vendor has NO access to this same custom field, and the custom field doesn't show up in their public profile.
To clarify, my goal is to add a custom field to a Woocommerce Product Vendor's public profile that can be edited by the vendor.
Thanks!
add_action('wcpv_product_vendors_edit_form_fields', 'edit_vendor_custom_fields', 10);
function edit_vendor_custom_fields($term) {
$vendor_data = get_term_meta( $term->term_id, 'vendor_data', true );
$custom_field = isset( $vendor_data['custom_field'] ) ? $vendor_data['custom_field'] : '';
?>
<tr class="form-field">
<th scope="row" valign="top"><label for="custom_field">test</label></th>
<td>
<input type="text" name="vendor_data[custom_field]" id="custom_field" value="<?php echo esc_attr($custom_field); ?>" />
</td>
</tr>
<?php
}

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.

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.

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

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.

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