How to add custom field in edit user page - php

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;
}

Related

How to add a file uploading field in WordPress default registration page?

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.

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'] );
}

Additional image field in WooCommerce category

With help i've made custom field on WooCommerce's category, but i want to make it media field (to pick cover image and upload directly from form).
I'm using "How do I add custom fields to the categories in Woocommerce?"
// Add term page
function custom_product_taxonomy_add_new_meta_field() {
// this will add the custom meta field to the add new term page
?>
<div class="form-field">
<label for="term_meta[category_cover]"><?php _e( 'Category Cover', 'category-cover' ); ?></label>
<input type="text" name="term_meta[category_cover]" id="term_meta[category_cover]" value="">
<p class="description"><?php _e( 'Enter a value for this field','category-cover' ); ?></p>
</div>
<?php
}
add_action( 'product_cat_add_form_fields', 'custom_product_taxonomy_add_new_meta_field', 10, 2 );
// Edit term page
function custom_product_taxonomy_edit_meta_field($term) {
// put the term ID into a variable
$t_id = $term->term_id;
// retrieve the existing value(s) for this meta field. This returns an array
$term_meta = get_option( "taxonomy_$t_id" ); ?>
<tr class="form-field">
<th scope="row" valign="top"><label for="term_meta[category_cover]"><?php _e( 'Category Cover', 'category-cover' ); ?></label></th>
<td>
<input type="text" name="term_meta[category_cover]" id="term_meta[category_cover]" value="<?php echo esc_attr( $term_meta['category_cover'] ) ? esc_attr( $term_meta['category_cover'] ) : ''; ?>">
<p class="description"><?php _e( 'Enter an URL for category cover','category-cover' ); ?></p>
</td>
</tr>
<?php
}
add_action( 'product_cat_edit_form_fields', 'custom_product_taxonomy_edit_meta_field', 10, 2 );
// Save extra taxonomy fields callback function.
function save_taxonomy_custom_meta( $term_id ) {
if ( isset( $_POST['term_meta'] ) ) {
$t_id = $term_id;
$term_meta = get_option( "taxonomy_$t_id" );
$cat_keys = array_keys( $_POST['term_meta'] );
foreach ( $cat_keys as $key ) {
if ( isset ( $_POST['term_meta'][$key] ) ) {
$term_meta[$key] = $_POST['term_meta'][$key];
}
}
// Save the option array.
update_option( "taxonomy_$t_id", $term_meta );
}
}
add_action( 'edited_product_cat', 'save_taxonomy_custom_meta', 10, 2 );
add_action( 'create_product_cat', 'save_taxonomy_custom_meta', 10, 2 );
I dont know what should I tell more, but i will be really thankful for help me! :)

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()

Wordpress - Saving Custom User Profile 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');

Categories