On the Users.php page I need an automatic filter to list only one type of user, without having to select an accurate filter that opens the page and is already with this filter
Try this inside your functions file:
Create a custom field, "Company Name". It is shown on both Add/Update user screens.
Hook “user_new_form”, will display the field on Add New User screen.
function custom_user_profile_fields($user){
if(is_object($user))
$company = esc_attr( get_the_author_meta( 'company', $user->ID ) );
else
$company = null;
?>
<h3>Extra profile information</h3>
<table class="form-table">
<tr>
<th><label for="company">Company Name</label></th>
<td>
<input type="text" class="regular-text" name="company" value="<?php echo $company; ?>" id="company" /><br />
<span class="description">Where are you?</span>
</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" );
Save the custom field in database:
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_user_meta($user_id, 'company', $_POST['company']);
}
add_action('user_register', 'save_custom_user_profile_fields');
add_action('profile_update', 'save_custom_user_profile_fields');
Related
hope someone can help. I found the following code on the web and it works well to create a new custom field which I can input from the WP 'users' tab. Yet I need to give this field manually a value in php instead trough an input form. Thanks!
function custom_user_profile_fields($user){
if(is_object($user))
$company = esc_attr( get_the_author_meta( 'company', $user->ID ) );
else
$company = null;
?>
<h3>Extra profile information</h3>
<table class="form-table">
<tr>
<th><label for="company">Company Name</label></th>
<td>
<input type="text" class="regular-text" name="company" value="<?php echo $company; ?>" id="company" /><br />
<span class="description">Where are you?</span>
</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_user_meta($user_id, 'company', $_POST['company']);
}
add_action('user_register', 'save_custom_user_profile_fields');
add_action('profile_update', 'save_custom_user_profile_fields');
If you are looking to save a fixed value to each company field to the usermeta table in the WordPress database, you can modify your saving function, to provide a fixed value instead of the POST field, you are assigning in your code snippet right now.
update_user_meta(
user_id: $user_id,
meta_key: 'company',
meta_value: 'your-custom-fiexed-value-here', // <--- here you go
);
(using named parameters requires PHP ^8.0.0)
WordPress docs: https://developer.wordpress.org/reference/functions/update_user_meta/
Also, note that there is a special Stack Exchange for WordPress Q&A: https://wordpress.stackexchange.com/
I'm facing a problem. I need to create some woocommerce customer role accounts manualy from the wp-admin. What I need to do is to add a woocommerce billing_phone number into the form which will be saved into the database and added to woocommerce my account phone field automatically. I've tried to do it with the code bellow but it did not help. Any ideas how could I make it?
I need to add it to user-new.php ...I know there is possibility to add the billing_phone after I create the user account, but I want to save time by searching for the account and doing things twice so I want to save time by adding this field directly into the account creation process from wp-admin.
Thank you in advance!
Code I used but did not help:
add_action( 'user_new_form', function( $type ){
if( 'add-new-user' !== $type )
return;
echo '<input type="text" name="billing_phone" id="billing_phone" value=""/>';
});
This is actually very easy to do. So the way WooCommerce autofills those fields is if by fetching the actually billing and shipping addresses for the user. If we manage so save the users contact number on regster hook we can make this work.
We will first create a field to save mobile data.
add_action( 'user_new_form', 'bks_add_mobile_field' );
add_action( 'edit_user_profile', 'bks_add_mobile_field' );
add_action( 'show_user_profile', 'bks_add_mobile_field' );
function bks_add_mobile_field( $user ) { ?>
<h3><?php _e("Extra profile information", "blank"); ?></h3>
<table class="form-table">
<tr>
<th><label for="mobile"><?php _e("Mobile Number"); ?></label></th>
<td>
<input type="text" name="mobile" id="mobile" value="<?php echo esc_attr( get_the_author_meta( 'mobile', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e("Please enter mobile."); ?></span>
</td>
</tr>
</table>
<?php }
Now we would save this mobile in both the fields mobile and billing_mobile.
add_action( 'user_register', 'bks_save_mobile_field' );
function bks_save_mobile_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 ( isset( $_POST['mobile'] ) ) {
update_user_meta($user_id, 'mobile', $_POST['mobile']);
update_user_meta($user_id, 'billing_phone', $_POST['mobile']);
}
}
So to put it all together add the following code in your functions.php file.
add_action( 'user_new_form', 'bks_add_mobile_field' );
add_action( 'show_user_profile', 'bks_add_mobile_field' );
add_action( 'edit_user_profile', 'bks_add_mobile_field' );
function bks_add_mobile_field( $user ) { ?>
<h3><?php _e("Extra profile information", "blank"); ?></h3>
<table class="form-table">
<tr>
<th><label for="mobile"><?php _e("Mobile Number"); ?></label></th>
<td>
<input type="text" name="mobile" id="mobile" value="<?php echo esc_attr( get_the_author_meta( 'mobile', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e("Please enter mobile."); ?></span>
</td>
</tr>
</table>
<?php }
add_action( 'user_register', 'bks_save_mobile_field' );
function bks_save_mobile_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 ( isset( $_POST['mobile'] ) ) {
update_user_meta($user_id, 'mobile', $_POST['mobile']);
update_user_meta($user_id, 'billing_phone', $_POST['mobile']);
}
}
This code is tested and it WORKS.
In woocommerce, after creating and registering some custom account fields in a custom php file using the following code:
<label for="reg_shipping_phone"> ... </label>
<input class="..." id="reg_shipping_phone" name="shipping_phone"
type="tel" pattern="[a-z0-9.-]{6,40}"
title=" ... " placeholder=" ... "
value="<?php esc_attr_e( $_POST['shipping_phone'] ); ?>" />
and in my theme's function.php file:
//save on admin/&frontend on change
add_action( 'personal_options_update', 'woo_save_extra_register_fields' );
add_action( 'woocommerce_save_account_details', 'woo_save_extra_register_fields' );
// Save extra registration fields data on from
add_action( 'woocommerce_created_customer', 'woo_save_extra_register_fields' );
function woo_save_extra_register_fields( $customer_id )
{
if( isset( $_POST['shipping_phone'] ) ) {
update_user_meta( $customer_id, 'shipping_phone', $_POST['shipping_phone'] );
}
}
I would like to implement this "Shipping phone" field in the correct position on Backend User profile pages inside the "Customer's shipping address" section table
How can I insert additional fields in the "Customer's shipping address" section table?
For example I have tried the following *(but 11,5 is wrong...)*:
add_action( 'show_user_profile', 'extra_profile_fields',***11,5*** );
add_action( 'edit_user_profile', 'extra_profile_fields',***11,5*** );
function extra_profile_fields( $user ) { ?>
<h3>NAME OF TABLE</h3>
<table class="form-table">
<tr>
<th><label for="shipping_phone">SHIPPING PHONE</label></th>
<td>
<input type="text" name="shipping_phone" id="shipping_phone" value="<?php echo esc_attr( $user->shipping_phone); ?>" class="regular-text" />
</td>
</tr>
>
</table>
<?php }
Any help is appreciated.
If you want to include your custom "Shipping phone" field in Backend User profile under "Customer's shipping address" section, you will use the following instead:
add_filter( 'woocommerce_customer_meta_fields', 'filter_add_customer_meta_fields', 10, 1 );
function filter_add_customer_meta_fields( $args ) {
$args['shipping']['fields']['shipping_phone'] = array(
'label' => __( 'Shipping phone', 'woocommerce' ),
'description' => '',
);
return $args;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
This little code snippet will do it all. If you change the value in this "Shipping phone" field, it will be updated without needing any additional code.
I am trying to add a custom field to the backend of wordpress in user profiles. I want it to get the date registered, but then have it so you can edit the field and overwrite it?
Ive currently got it to pull the date registered but need a way of making it so it just initially pulls the date registered value but will overwrite if modified?
add_action( 'show_user_profile', 'extra_user_profile_fields' );
add_action( 'edit_user_profile', 'extra_user_profile_fields' );
function extra_user_profile_fields( $user ) { ?>
<h3>Date Registered</h3>
<?php $userr_ID = get_current_user_id(); ?>
<table class="form-table">
<tr>
<th><label for="date_reg"><?php _e("Date Registered"); ?></label></th>
<td>
<input type="text" name="datereg" id="datereg" value="<?php echo the_author_meta( 'user_registered', $userr_ID ); ?>" class="regular-text" /><br />
</td>
</tr>
</table>
<?php }
Old broken code.
add_action('edit_user_profile_update', 'update_extra_profile_fields');
function update_extra_profile_fields($userID) {
if ( current_user_can('edit_user',$userID) )
update_user_meta($userID, 'user_registered', current_time( 'mysql' ));
}
function extra_user_profile_fields( $user ) {
?>
<h3>Date Registered</h3>
<table class="form-table">
<tr>
<th><label for="date_reg"><?php _e("Date Registered"); ?></label></th>
<td>
<input type="text" name="datereg" id="datereg" value="<?php echo get_the_author_meta( 'user_registered', $user->ID); ?>" class="regular-text" /><br />
</td>
</tr>
</table>
<?php
}
add_action( 'show_user_profile', 'extra_user_profile_fields', 10, 1 );
add_action( 'edit_user_profile', 'extra_user_profile_fields', 10, 1 );
Updated working code.
add_action('profile_update', 'update_extra_profile_fields', 10, 2);
function update_extra_profile_fields($userID, $oldData) {
if ( current_user_can('edit_user',$userID) ) {
$updattionDate = date('Y-m-d H:i:s');
$update = update_user_meta($userID, 'user_registered', $updattionDate);
}
}
function extra_user_profile_fields( $user ) {
?>
<h3>Date Registered</h3>
<table class="form-table">
<tr>
<th><label for="date_reg"><?php _e("Date Registered"); ?></label></th>
<td>
<?php
$registrationTime = get_user_meta($user->ID, 'user_registered', true);;
$registrationDate = date('Y-m-d H:i:s', strtotime(date($registrationTime)));
?>
<input type="text" name="datereg" id="datereg" value="<?php echo $registrationDate; ?>" class="regular-text" /><br />
</td>
</tr>
</table>
<?php
}
add_action( 'show_user_profile', 'extra_user_profile_fields', 10, 1 );
add_action( 'edit_user_profile', 'extra_user_profile_fields', 10, 1 );
I noticed an error in my profile updation code namely the wrong hook. The correct for user profile update event is profile_update and not edit_user_profile_update
Even with this fixed you will still not see the correct registration date as we are calling the wrong function for it. We were retrieving date using get_the_author_meta which retrieves the data of the author of current post. The correct function for this is get_user_meta. So the updated code written above is working properly.
Below are the screenshot of the same code implemented in one of my projects.
Functions File:
Before Profile Updated
After Profile Updated
I am searching all over the internet but can't find how to add or update meta data after the user has activated their blog/site.
I want to add those one so that the user can see their additional meta/custom field in their profile page.
I have this code on my wp-signup.php file:
$signup_meta = array(
'organization_address'=>$_POST['organization_address'],
'organization_name'=>$_POST['organization_name'],
'organization_number'=>$_POST['organization_number'],
'organization_address'=>$_POST['organization_address'],
'organization_zip'=>$_POST['organization_zip'],
'organization_city'=>$_POST['organization_city'],
'organization_country'=>$_POST['organization_country'],
'first_name'=>$_POST['first_name'],
'last_name'=>$_POST['last_name'],
'phone'=>$_POST['phone'],
'lang_id' => 1,
'public' => $public
);
wpmu_signup_blog($domain, $path, $blog_title, $email, $email, $signup_meta);
add_action('user_register', 'custom_register_extra_fields');
add_action('wpmu_activate_user', 'stjert_register_user_meta', 10, 3);
$meta_defaults = apply_filters( 'signup_create_blog_meta', $signup_meta );
confirm_blog_signup($domain, $path, $blog_title, $email, $email, $signup_meta);
Then on my functions.php file , I have this code:
add_action ( 'show_user_profile', 'my_show_extra_profile_fields' );
add_action ( 'edit_user_profile', 'my_show_extra_profile_fields' );
function my_show_extra_profile_fields ( $user )
{
<h3>Kontaktinformasjon</h3>
<table class="form-table">
<tr>
<th><label for="first_name">Fornavn:</label></th>
<td>
<input type="text" name="first_name" id="first_name" value="<?php echo esc_attr( get_the_author_meta( 'first_name', $user->ID ) ); ?>" class="regular-text" />
</td>
</tr>
<tr>
<th><label>Etternavn:</label></th>
<td>
<input type="text" name="last_name" id="last_name" value="<?php echo esc_attr( get_the_author_meta( 'last_name', $user->ID ) ); ?>" class="regular-text" />
</td>
</tr>
<tr>
<th><label>Telefon:</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" />
</td>
</tr>
</table>
I cut down the code because its too long.
When I go to the Profile Page(wp-admin), I don't see those custom fields/meta values that I had during registration, I was thinking that maybe it was not saved or whatever?
Any help on this? Please. I am really new to wordpress and the 'Wordpress way' is very difficult for me. Feels like very different from MVC frameworks.
Your help will be greatly appreciated!
Thanks!
PS: Here's my entire code of wp-signup.php http://pastebin.com/YcvHeHkC
I don't think I have fully understood the question here...But what I see, is that you're trying to do a registration form, with non-regular fields and save those as metadata. Your way of thinking is correct, but the functions I'm not pretty sure...
It was not clear either if you want to do this inside or outside the admin panel, so, what i suggest is
Create the user retriving it's Username, Password and Email, using
wp_create_user( $username, $password, $email ); // Go to codex
http://codex.wordpress.org/Function_Reference/wp_create_user
This function will return the ID of the given user created, than you get all this posts and in a foreach or something use this other function
add_user_meta( $user_id, $meta_key, $meta_value, $unique ); // Go to codex
http://codex.wordpress.org/Function_Reference/add_user_meta
And finally, on the edit page, or where you want to show it, you're able to show it by using
get_user_meta($user_id, $key, $single); // Go to codex
http://codex.wordpress.org/Function_Reference/get_user_meta