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()
Related
I have tried this method creating a separate plugin, but it is not working, can anyone help me out with this? Please. I have a restricted site. without filling out proper legal documents the user will not be approved from the WordPress dashboard. But when submitting for registration everything is working well, but the file is not uploading or not showing on the dashboard use profile page.
`
add_action( 'register_form', 'crf_registration_form' );
function crf_registration_form() {
$year = ! empty( $_POST['legal_documents'] ) ? intval( $_POST['legal_documents'] ) : '';
?>
<p>
<label for="legal_documents"><?php esc_html_e( 'National ID/Passport/Driving License', 'crf' ) ?><br/>
<input type="file"
id="legal_documents"
name="legal_documents"
value="<?php echo esc_attr( $documents ); ?>"
class="input"
/>
</label>
</p>
<?php
}
add_filter( 'registration_errors', 'crf_registration_errors', 10, 3 );
function crf_registration_errors( $errors, $sanitized_user_login, $user_email ) {
if ( empty( $_POST['legal_documents'] ) ) {
$errors->add( 'legal_documents_error', __( '<strong>ERROR</strong>: Please upload your legal documents to have active account.', 'crf' ) );
}
return $errors;
}
add_action( 'user_register', 'crf_user_register' );
function crf_user_register( $user_id ) {
if ( ! empty( $_POST['legal_documents'] ) ) {
update_user_meta( $user_id, 'legal_documents', intval( $_POST['legal_documents'] ) );
}
}
/**
* Back end registration
*/
add_action( 'user_new_form', 'crf_admin_registration_form' );
function crf_admin_registration_form( $operation ) {
if ( 'add-new-user' !== $operation ) {
// $operation may also be 'add-existing-user'
return;
}
$year = ! empty( $_POST['legal_documents'] ) ? intval( $_POST['legal_documents'] ) : '';
?>
<h3><?php esc_html_e( 'Verification documents', 'crf' ); ?></h3>
<table class="form-table">
<tr>
<th><label for="legal_documents"><?php esc_html_e( 'legal_documents', 'crf' ); ?></label> <span class="description"><?php esc_html_e( '(required)', 'crf' ); ?></span></th>
<td>
<input type="file"
id="legal_documents"
name="legal_documents"
value="<?php echo esc_attr( $documents ); ?>"
class="regular-text"
/>
</td>
</tr>
</table>
<?php
}
add_action( 'user_profile_update_errors', 'crf_user_profile_update_errors', 10, 3 );
function crf_user_profile_update_errors( $errors, $update, $user ) {
if ( $update ) {
return;
}
if ( empty( $_POST['legal_documents'] ) ) {
$errors->add( 'legal_documents_error', __( '<strong>ERROR</strong>: Please upload your valid documents', 'crf' ) );
}
}
add_action( 'edit_user_created_user', 'crf_user_register' );
/**
* Profile Display
*/
add_action( 'show_user_profile', 'crf_show_extra_profile_fields' );
add_action( 'edit_user_profile', 'crf_show_extra_profile_fields' );
function crf_show_extra_profile_fields( $user ) {
?>
<h2><?php esc_html_e( 'Verification documents', 'crf' ); ?></h2>
<table class="form-table">
<tr>
<th><label for="legal_documents"><?php esc_html_e( 'Legal Documents', 'crf' ); ?></label></th>
<td><?php echo esc_html( get_the_author_meta( 'legal_documents', $user->ID ) ); ?></td>
</tr>
</table>
<?php
}
`
I have tried this code snippets but the file is not receiving on the WordPress user page, it is showing 0.
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'] );
}
I want to add a upload profile image field and save it on My account > edit account without any plugin in woocommerce.
I added the below code in functions.php to add the profile picture on the my account page.
add_action( 'woocommerce_edit_account_form', 'add_profile_imageto_edit_account_form' );
function add_profile_imageto_edit_account_form() {
$user = wp_get_current_user();
?>
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="favorite_color"><?php _e( 'Upload Profile Photo', 'woocommerce' ); ?> </label>
<input type="file" name="profile_image" id="profile_image" placeholder="Upload Profile Photo" />
</p>
<?php
}
Suggest me anybody how to do save this data and display the saved data in edit account page.
You have 3 options to display a new field
As the first field using woocommerce_edit_account_form_start hook (used in my code)
After existing fields using woocommerce_edit_account_form hook
In a specific location, overriding myaccount/form-edit-account.php template file.
Functions used: to display the image.
wp_get_attachment_url - Retrieve the URL for an attachment.
wp_get_attachment_image - Get an HTML img element representing an image attachment
Additional CSS for the layout may be desired
// Add field
function action_woocommerce_edit_account_form_start() {
?>
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="image"><?php esc_html_e( 'Image', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="file" class="woocommerce-Input" name="image" accept="image/x-png,image/gif,image/jpeg">
</p>
<?php
}
add_action( 'woocommerce_edit_account_form_start', 'action_woocommerce_edit_account_form_start' );
// Validate
function action_woocommerce_save_account_details_errors( $args ){
if ( isset($_POST['image']) && empty($_POST['image']) ) {
$args->add( 'image_error', __( 'Please provide a valid image', 'woocommerce' ) );
}
}
add_action( 'woocommerce_save_account_details_errors','action_woocommerce_save_account_details_errors', 10, 1 );
// Save
function action_woocommerce_save_account_details( $user_id ) {
if ( isset( $_FILES['image'] ) ) {
require_once( ABSPATH . 'wp-admin/includes/image.php' );
require_once( ABSPATH . 'wp-admin/includes/file.php' );
require_once( ABSPATH . 'wp-admin/includes/media.php' );
$attachment_id = media_handle_upload( 'image', 0 );
if ( is_wp_error( $attachment_id ) ) {
update_user_meta( $user_id, 'image', $_FILES['image'] . ": " . $attachment_id->get_error_message() );
} else {
update_user_meta( $user_id, 'image', $attachment_id );
}
}
}
add_action( 'woocommerce_save_account_details', 'action_woocommerce_save_account_details', 10, 1 );
// Add enctype to form to allow image upload
function action_woocommerce_edit_account_form_tag() {
echo 'enctype="multipart/form-data"';
}
add_action( 'woocommerce_edit_account_form_tag', 'action_woocommerce_edit_account_form_tag' );
To display the image (can be used anywhere, provided you adjust the desired hook)
// Display
function action_woocommerce_edit_account_form() {
// Get current user id
$user_id = get_current_user_id();
// Get attachment id
$attachment_id = get_user_meta( $user_id, 'image', true );
// True
if ( $attachment_id ) {
$original_image_url = wp_get_attachment_url( $attachment_id );
// Display Image instead of URL
echo wp_get_attachment_image( $attachment_id, 'full');
}
}
add_action( 'woocommerce_edit_account_form', 'action_woocommerce_edit_account_form' );
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;
}
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');