Created
May 4, 2022 18:00
-
-
Save max-kk/56b0667eb1615763c11679edc05df061 to your computer and use it in GitHub Desktop.
LRM custom_uploads
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Copy after | |
add_filter('lrm/additional_atts', function( $atts ) { | |
$atts['enctype'] = 'multipart/form-data'; | |
return $atts; | |
}); | |
global $lrm_files; | |
$lrm_files = [ | |
'passport' => 'Passport', | |
'trading_license' => 'Trading license', | |
]; | |
add_action( 'lrm/register_form', 'lrm_add_woo_account_registration_fields2' ); | |
function lrm_add_woo_account_registration_fields2() { | |
global $lrm_files; ?> | |
<?php foreach ( $lrm_files as $file => $label ) : ?> | |
<p class="form-row validate-required" id="image"> | |
<label for="<?php echo esc_attr($file); ?>" class=""><?php echo $label; ?> <abbr class="required" title="required">*</abbr></label> | |
<input type='file' name='<?php echo esc_attr($file); ?>' accept='image/*,.pdf' required> | |
</p> | |
<?php endforeach; | |
} | |
// -------------- | |
// 2. Validate new field | |
add_filter( 'registration_errors', 'lrm_validate_woo_account_registration_fields2', 10, 3 ); | |
function lrm_validate_woo_account_registration_fields2( $errors, $username, $email ) { | |
global $lrm_files; | |
foreach ( $lrm_files as $file => $label ) : | |
if ( !isset( $_FILES[$file] ) || empty( $_FILES[$file] ) ) { | |
$errors->add( $file.'_error', sprintf('Please provide a valid file for "%s"', $label) ); | |
} | |
endforeach; | |
return $errors; | |
} | |
// -------------- | |
// 3. Save new field | |
add_action( 'user_register', 'lrm_save_woo_account_registration_fields', 1 ); | |
function lrm_save_woo_account_registration_fields( $customer_id ) { | |
if ( !empty( $_FILES ) ) { | |
global $lrm_files; | |
foreach ( $lrm_files as $file => $label ) { | |
if ( empty($_FILES[$file]) ) { | |
continue; | |
} | |
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($file, 0); | |
if (is_wp_error($attachment_id)) { | |
update_user_meta($customer_id, $file, $_FILES[$file] . ": " . $attachment_id->get_error_message()); | |
} else { | |
update_user_meta($customer_id, $file, $attachment_id); | |
} | |
} | |
} | |
} | |
add_action( 'show_user_profile', 'lrm_display_user_custom_data' ); | |
add_action( 'edit_user_profile', 'lrm_display_user_custom_data' ); | |
function lrm_display_user_custom_data( $user ) { | |
global $lrm_files; | |
?> | |
<h3>Files</h3> | |
<table class="form-table"> | |
<?php foreach ( $lrm_files as $file => $label ) : ?> | |
<tr> | |
<th><label><?php echo $label; ?></label></th> | |
<td> | |
<?php if ( get_user_meta( $user->ID, $file, true ) ) : ?> | |
<?php echo get_user_meta( $user->ID, $file, true ); ?><br/> | |
<img src="<?php echo esc_attr(get_user_meta( $user->ID, $file, true )); ?>"/> | |
<?php else: ?> | |
No image | |
<?php endif; ?> | |
</td> | |
</tr> | |
<?php endforeach; ?> | |
</table> | |
<?php | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment