Wordpress - Saving Custom User Profile Fields - php

I am currently trying to add some custom user profile fields for my Wordpress users.
I have added the following code into my functions.php but for some reason the data entered is not saving...
//** CUSTOM USER META **//
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>Extra profile information</h3>
<table class="form-table">
<tr>
<th><label for="club">Club You Support</label></th>
<td>
<input type="text" name="club" id="club" value="<?php echo esc_attr( get_the_author_meta( 'club', $user->ID ) ); ?>" class="regular-text" /><br />
</td>
</tr>
</table>
<?php }
add_action( 'personal_options_update', 'my_save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'my_save_extra_profile_fields' );
function my_save_extra_profile_fields( $user_id ) {
update_usermeta( $user_id, 'club', sanitize_text_field( $_POST['club']) );
}
Any ideas as to why this data isn't sticking ?

There is an easier and proper way to create new profile fields in Wordpress. Based on your code above, try dropping the code below on your functions.php file on your theme:
function my_show_extra_profile_fields {
$user_contact_method['club'] = 'Club You Support';
return $user_contact_method;
}
add_filter( 'user_contactmethods', 'my_show_extra_profile_fields' );
This will automatically create the new fields on your profile page and accordingly save them to the data base as custom fields (meta) for user.
You can display this info on your theme using the_author_meta('club');

Related

adding custom File field to Woocommerce dokan plugin

I’m trying to add an extra field on dokan registration for vendors to upload an identification document.. i found the code specified for adding an extra field to the form but it is for a "text" input type… i changed the input type from "text" to "file"
<p class="form-row form-group form-row-wide">
<label for="veri-file"><?php esc_html_e( 'Upload Verification ID', 'dokan-custom-codes' ); ?><span class="required">*</span></label>
<input type="file" class="verifile" name="veri_file" id="veri_file" accept="image/png, image/jpeg" value="upload"<?php if ( ! empty( $postdata['veri_file'] ) ) echo esc_attr($postdata['veri_file']); ?>" required="required" />
</p>
and that worked… but the code to save and display the field content on the user backend doesn’t show the image just the same upload box
// save id verification field
I also used below code to show in admin side but image not shown not move to folders
function dokan_custom_seller_registration_required_fields( $required_fields ) {
$required_fields['veri_file'] = __( 'Please upload a valid means of Identification', 'dokan-custom' );
return $required_fields;
};
add_filter( 'dokan_seller_registration_required_fields', 'dokan_custom_seller_registration_required_fields' );
function dokan_custom_new_seller_created( $vendor_id, $dokan_settings ) {
$post_data = wp_unslash( $_POST );
$veri_file = $post_data['veri_file'];
update_user_meta( $vendor_id, 'dokan_custom_veri_file', $veri_file );
}
add_action( 'dokan_new_seller_created', 'dokan_custom_new_seller_created', 10, 2 );
/* Add custom profile fields (call in theme : echo $curauth->fieldname;) */
add_action( 'dokan_seller_meta_fields', 'my_show_extra_profile_fields' );
function my_show_extra_profile_fields( $user ) { ?>
<?php if ( ! current_user_can( 'manage_woocommerce' ) ) {
return;
}
if ( ! user_can( $user, 'dokandar' ) ) {
return;
}
$gst = get_user_meta( $user->ID, 'dokan_custom_veri_file', true );
?>
<tr>
<th><?php esc_html_e( 'Upload Verification ID', 'dokan-lite' ); ?></th>
<td>
<input type="file" name="veri_file" class="verifile" value="<?php echo esc_attr($gst); ?>"/>
<img src=".$gst." height=200 width=300 />
</td>
</tr>
echo "<img src=".$gst." height=200 width=300 />";
<?php
}
add_action( 'personal_options_update', 'my_save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'my_save_extra_profile_fields' );
function my_save_extra_profile_fields( $user_id ) {
if ( ! current_user_can( 'manage_woocommerce' ) ) {
return;
}
update_usermeta( $user_id, 'dokan_custom_veri_file', $_POST['veri_file'] );
}

How to Get Value from input fields in cart page to admin order details page?

In the cart page, I already created two text fields, but I'm unable to get the value that user input in that fields to admin order details page.
How can I get the values in order details page and I also want to save that details in the database.
Below is the code of my cart page
<?php
do_action( 'woocommerce_before_cart' ); ?>
<section class="checkout_display">
<div class="container">
<div class="row">
<form action="<?php echo esc_url( wc_get_checkout_url() );?>" method="post">
<?php do_action( 'woocommerce_before_cart_table' ); ?>
<div class="col-lg-6 col-md-8 col-sm-12 inset">
<div class="checkout_title">get started</div>
<div class="first_form">
<!--Code which display text field one -->
<div class="form-group" >
<label>Instagram username</label>
<input type="text" name="igusername" required>
</div>
<!--Code which display text field second -->
<div class="form-group">
<label>Email</label>
<input type="email" name="useremail" required>
</div>
<?php do_action( 'woocommerce_before_cart_contents' ); ?>
<?php
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
$product_id = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key );
if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters( 'woocommerce_cart_item_visible', true, $cart_item, $cart_item_key ) ) {
$product_permalink = apply_filters( 'woocommerce_cart_item_permalink', $_product->is_visible() ? $_product->get_permalink( $cart_item ) : '', $cart_item, $cart_item_key );
?>
<div class="form-group">
<label>Your package</label>
<select disabled>
<option> <?php echo wp_kses_post( apply_filters( 'woocommerce_cart_item_name', sprintf( '%s', esc_url( $product_permalink ), $_product->get_name() ), $cart_item, $cart_item_key ) );?> For
<span><?php echo apply_filters( 'woocommerce_cart_item_price', WC()->cart->get_product_price( $_product ), $cart_item, $cart_item_key ); // PHPCS: XSS ok.
?>
</span>
</option>
</select>
</div>
<div class="checkbox"><input type="checkbox" /> Yes! send me special promotion and discounts</div>
<div class="btn">
<input type="submit" value="next">
</a>
</div>
<?php
}
}
?>
<?php do_action( 'woocommerce_cart_contents' ); ?>
<?php do_action( 'woocommerce_after_cart_contents' ); ?>
</div>
</div>
<?php do_action( 'woocommerce_after_cart_table' ); ?>
</form>
</div>
</div>
</section>
<?php do_action( 'woocommerce_after_cart' ); ?>
Code which display text field one and second are the text box from where i want to get value which user input and want to display and store them in admin order details page after completion of payment
Try the following that will display your posted fields values to Woocommerce session. When order will be placed, it will saved that custom session data as custom order meta data and display it in admin orders:
// Save the posted data to Woocommerce session
add_filter( 'init', 'set_instagram_posted_data_to_wc_sessions', 10, 3 );
function set_instagram_posted_data_to_wc_sessions() {
if ( ( is_cart() || is_checkout() ) && isset($_POST['igusername']) && isset($_POST['useremail']) ) {
// Enable Woocommerce sessions (if not done yet)
if ( isset(WC()->session) && ! WC()->session->has_session() ) {
WC()->session->set_customer_session_cookie( true );
}
$session_data = []; // initializing
if( isset($_POST['igusername']) && ! empty($_POST['igusername']) ) {
// Add the dropdown value as custom cart item data
$session_data['ig_username'] = sanitize_text_field($_POST['igusername']);
}
if( isset($_POST['useremail']) && ! empty($_POST['useremail']) ) {
// Add the dropdown value as custom cart item data
$session_data['ig_useremail'] = sanitize_email($_POST['useremail']);
}
// Set the data to custom wc_sessions
if( sizeof($session_data) > 0 ) {
WC()->session->set('ig_data', $session_data);
}
}
}
// Save the session data as custom order meta data (post meta data)
add_action( 'woocommerce_checkout_create_order', 'action_checkout_create_order_callback', 10, 2 );
function action_checkout_create_order_callback( $order, $data ) {
if( $session_data = WC()->session->get('ig_data') ) {
$order->update_meta_data( '_ig_username', wc_clean($session_data['ig_username']) );
$order->update_meta_data( '_ig_useremail', wc_clean($session_data['ig_useremail']) );
// remove the data from Woocommerce session
WC()->session->__unset('ig_data'):
}
}
// Display custom data in Admin orders, below the billing address
add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_after_admin_order_billing_address', 10, 1 );
function display_after_admin_order_billing_address( $order ){
$ig_username = $order->get_meta('_ig_username');
$ig_useremail = $order->get_meta('_ig_useremail');
if( ! empty($ig_username) || ! empty($ig_useremail) ) :
echo '<div class="instagram-userdata">
<h3>'.__('Instagram user data').'</h3>
<table cellpadding="0" cellspacing="0" border="0" style="margin-top:6px;">
<tr><th align="left">'.__('Username').': </th><td> ' . $ig_username . '</td></tr>
<tr><th align="left">'.__('Email').': </th><td> ' . $ig_useremail . '</td></tr>
</table>
</div>';
endif;
}
Code goes in function.php file of your active child theme (or active theme). It should works now.
If it doesn't work with sessions you can use the following that will add the post data to hidden fields in checkout page and will post that data again when order is submitted… Everything else is the same as above…
So you can try alternatvelly:
// Display the posted data values in checkout hidden fields
add_filter( 'woocommerce_after_checkout_billing_form', 'set_instagram_posted_data_in_hidden_field', 10, 3 );
function set_instagram_posted_data_in_hidden_field() {
if ( isset($_REQUEST['igusername'])|| isset($_REQUEST['useremail']) ) {
// Display hidden fields with the Instagram posted values
?><input type="hidden" name="ig_username" value="<?php echo $_REQUEST['igusername']; ?>">
<input type="hidden" name="ig_useremail" value="<?php echo $_REQUEST['useremail']; ?>"><?php
}
}
// Save checkout hidden fields values as custom order meta data (post meta data)
add_action( 'woocommerce_checkout_create_order', 'action_checkout_create_order_callback', 10, 2 );
function action_checkout_create_order_callback( $order, $data ) {
if ( isset($_POST['ig_username']) {
$order->update_meta_data( '_ig_username', sanitize_text_field($_POST['ig_username']) );
}
if ( isset($_POST['ig_useremail']) {
$order->update_meta_data( '_ig_useremail', sanitize_email($session_data['ig_useremail']) );
}
}
// Display custom data in Admin orders, below the billing address
add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_after_admin_order_billing_address', 10, 1 );
function display_after_admin_order_billing_address( $order ){
$ig_username = $order->get_meta('_ig_username');
$ig_useremail = $order->get_meta('_ig_useremail');
if( ! empty($ig_username) || ! empty($ig_useremail) ) :
echo '<div class="instagram-userdata">
<h3>'.__('Instagram user data').'</h3>
<table cellpadding="0" cellspacing="0" border="0" style="margin-top:6px;">
<tr><th align="left">'.__('Username').': </th><td> ' . $ig_username . '</td></tr>
<tr><th align="left">'.__('Email').': </th><td> ' . $ig_useremail . '</td></tr>
</table>
</div>';
endif;
}
Code goes in function.php file of your active child theme (or active theme). It should works now.
In Order pages in backend, you will get something like:
To retrieve the data from $order the WC_Order Object (or $order_id the Order Id) use:
$order = wc_get_order( $order_id ); // (optionally if required) with the Order ID
$ig_username = $order->get_meta('_ig_username');
$ig_useremail = $order->get_meta('_ig_useremail');
You can use order meta to save anything to a particular order.
add_action('woocommerce_checkout_create_order',
'before_checkout_create_order', 20, 2);
function before_checkout_create_order( $order, $data )
{
$order->update_meta_data( '_custom_text1', 'value' );
$order->update_meta_data( '_custom_tex2', 'value' );
}
Add your text box values to the value section of the meta key and they will store in the DB when the order is saved.
They will appear at the bottom of the order screen in admin section

How to add custom field in edit user page

I would like to add new field in contact info section of edit user profile page.
I am using this code but it is adding new section after account management section but i want add it after email in contact info section.
<?php
add_action( 'show_user_profile', 'yoursite_extra_user_profile_fields' );
add_action( 'edit_user_profile', 'yoursite_extra_user_profile_fields' );
function yoursite_extra_user_profile_fields( $user ) {
?>
<h3><?php _e("User profile information", "blank"); ?></h3>
<table class="form-table">
<tr>
<th><label for="phone"><?php _e("Operational Manager Email"); ?></label></th>
<td>
<input type="text"
name="operational_email"
class="regular-text"
value="<?php echo esc_attr( get_the_author_meta( 'operational_email', $user->ID ) ); ?>"/>
<br />
<span class="description">
<?php _e("Please enter opertional manager email.">
</span>
</td>
</tr>
</table>
<?php
}
add_action( 'personal_options_update', 'yoursite_save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'yoursite_save_extra_user_profile_fields' );
function yoursite_save_extra_user_profile_fields( $user_id ) {
$saved = false;
if ( current_user_can( 'edit_user', $user_id ) ) {
update_user_meta(
$user_id,
'operational_email',
$_POST['operational_email']
);
$saved = true;
}
return true;
}
Well i had it done in my theme in recent project but i did it from the filter name 'user_contactmethods'
Read documentation : https://codex.wordpress.org/Plugin_API/Filter_Reference/contactmethods
add_filter( 'user_contactmethods', 'extra_contact_info' );
function extra_contact_info( $fields ) {
$fields['email'] = __( 'Operational Manager Email' );
return $fields;
}

How to add custom file field to user profile in wordpress?

I've found endless examples online on how to add extra custom fields to user profiles in wordpress. But non of them have shown how to add fields to upload files.
Heres what I got for my extra field:
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><?php _e("Extra Information", "blank"); ?></h3>
<table class="form-table">
<tr>
<th><label for="my_document"><?php _e("My Document"); ?></label></th>
<td>
<input type="file" name="my_document" id="my_document" value="<?php echo esc_attr( get_the_author_meta( 'my_document', $user->ID ) ); ?>" />
</td>
</tr>
</table>
<?php
}
Then for the form submit:
add_action( 'personal_options_update', 'yoursite_save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'yoursite_save_extra_user_profile_fields' );
function yoursite_save_extra_user_profile_fields( $user_id ) {
$saved = false;
if ( !current_user_can( 'edit_user', $user_id ) )
return false;
if(!empty($_FILES['my_document']['name'])) {
// Use the WordPress API to upload the file
$upload = wp_upload_bits($_FILES['my_document']['name'], null, file_get_contents($_FILES['my_document']['tmp_name']));
if(isset($upload['error']) && $upload['error'] != 0) {
wp_die('There was an error uploading your file. The error is: ' . $upload['error']);
} else {
add_post_meta($user_id, 'my_document', $upload);
update_post_meta($user_id, 'my_document', $upload);
} // end if/else
} // end if
}
The document isn't saving, I suspect the form in the edit profile hasn't the tags to upload files. I also don't know how to retrieve the document in the front end once its saved, as to show the user he has the file uploaded.
This works for me:
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><?php _e("Extra Information", "blank"); ?></h3>
<table class="form-table">
<tr>
<th scope="row">My Document</th>
<td><input type="file" name="my_document" value="" />
<?php
$doc = get_user_meta( $user->ID, 'my_document', true );
if (!isset($doc['error'])) {
$doc = $doc['url'];
echo "<img src='$doc' />";
} else {
$doc = $doc['error'];
echo $doc;
}
?>
</td>
</tr>
</table>
<?php
}
add_action( 'personal_options_update', 'yoursite_save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'yoursite_save_extra_user_profile_fields' );
function yoursite_save_extra_user_profile_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) )
return false;
if( $_FILES['my_document']['error'] === UPLOAD_ERR_OK ) {
$_POST['action'] = 'wp_handle_upload';
$upload_overrides = array( 'test_form' => false );
$upload = wp_handle_upload( $_FILES['my_document'], $upload_overrides );
update_user_meta( $user_id, 'my_document', $upload );
}
}
What do you see in your console when you dump the output?
var_dump($upload);
It could be related to the /tmp directory permissions (or existence).
I apologize if this isn't what you're asking, but have you tried ACF?
Here is how to get fields from a User field group.
Two more things to add to simplethemes's solution:
Make sure the <form> on the front end has the enctype="multipart/form-data" attribute.
Make sure you add require_once( ABSPATH . 'wp-admin/includes/file.php' ); before using the function wp_handle_upload()

Add custom fields to Author Information in WordPress

I'm looking for a way to add custom fields and display them (without plugin).
I've found a great example on the web. The author adds a number of custom fields by adding the following function to the fuctions.php file:
function modify_contact_methods($profile_fields) {
// Add new fields
$profile_fields['linkedin'] = 'LinkedIn URL';
$profile_fields['telephone'] = 'Telephone';
return $profile_fields;
}
add_filter('user_contactmethods', 'modify_contact_methods');
I've been able to successfully add such fields to the Contact Information section of my user registration form. I've been trying to add custom fields to other sections, like the Author Information section (where the Bio is), but without success.
I think that I've to change the value user_contactmethods in the add_filter(...) function, but I have not been able to find anything.
I do not even know if this is the correct way to do this, but it worked so far.
As you are new to wordpress, You don't have knowledge about the filter and action. If you go through the filter list , You will find user_contactmethods here.
As you can see in Author and User Filters, there are only 4 filters for Author and User. And we can use none of them to achieve your desired output.
But somehow we can do it by adding another field under the About the User something like Author Information.
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><?php _e("Author Information", "blank"); ?></h3>
<table class="form-table">
<tr>
<th><label for="author"><?php _e("Author Information"); ?></label></th>
<td>
<textarea name="author" id="author" rows="5" cols="10" ><?php echo esc_attr( get_the_author_meta( 'author', $user->ID ) ); ?></textarea><br />
<span class="description"><?php _e("Please enter Author's Information."); ?></span>
</td>
</tr>
</table>
<?php }
add_action( 'personal_options_update', 'save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_user_profile_fields' );
function save_extra_user_profile_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) ) { return false; }
update_user_meta( $user_id, 'author', $_POST['author'] );
}
So in that way you can add as many field as you want.

Categories