If you want to add user profile image by custom URL and add custom fields of profile image like first name, last name in your woo-commerce registration form then see our code below, you can also get this code in a custom user profile image URL plug-in.

<?php

/**
* Show the new image field in the user profile page.
*
* @param object $user User object.
*/
function wet_profile_img_fields( $user ) {
if ( ! current_user_can( ‘upload_files’ ) ) {
return;
}

// vars
$url = get_the_author_meta( ‘wet_meta’, $user->ID );
$upload_url = get_the_author_meta( ‘wet_upload_meta’, $user->ID );
$upload_edit_url = get_the_author_meta( ‘wet_upload_edit_meta’, $user->ID );
$button_text = $upload_url ? ‘Change Image’ : ‘Change Image’;

if ( $upload_url ) {
$upload_edit_url = get_site_url() . $upload_edit_url;
}
?>

<div id=”wet_container”>
<h3><?php _e( ‘User Profile Photo’, ‘user-profile-photo’ ); ?></h3>

<table class=”form-table”>
<tr>
<td>
<!– Outputs the image after save –>
<div id=”current_img”>
<?php if ( $upload_url ): ?>
<img class=”wet-current-img” src=”<?php echo esc_url( $upload_url ); ?>”/>

<div class=”edit_options uploaded”>
<a class=”remove_img”>
<span><?php _e( ‘Remove’, ‘user-profile-photo’ ); ?></span>
</a>

<a class=”edit_img” href=”<?php echo esc_url( $upload_edit_url ); ?>” target=”_blank”>
<span><?php _e( ‘Edit’, ‘user-profile-photo’ ); ?></span>
</a>
</div>
<?php elseif ( $url ) : ?>
<img class=”wet-current-img” src=”<?php echo esc_url( $url ); ?>”/>
<div class=”edit_options single”>
<a class=”remove_img”>
<span><?php _e( ‘Remove’, ‘user-profile-photo’ ); ?></span>
</a>
</div>
<?php else : ?>
<img class=”wet-current-img placeholder”
src=”<?php echo esc_url( plugins_url( ‘user-profile-photo/img/placeholder.gif’ ) ); ?>”/>
<?php endif; ?>
</div>

<!– External URL –>

<input type=”radio” id=”external_option” name=”img_option” value=”external” class=”tog” checked>
<label
for=”external_option”><?php _e( ‘Use External URL’, ‘user-profile-photo’ ); ?></label><br>
</div>

<!– Outputs the text field and displays the URL of the image retrieved by the media uploader –>
<div id=”wet_external”>
<input class=”regular-text” type=”text” name=”wet_meta” id=”wet_meta”
value=”<?php echo esc_url_raw( $url ); ?>”/>
</div>

<!– Outputs the save button –>
<span class=”description”>
<?php
_e(
‘use a custom url for your user profile photo.’,
‘user-profile-photo’
);
?>
</span>
<p class=”description”>
<?php _e( ‘Update Profile to save your changes.’, ‘user-profile-photo’ ); ?>
</p>
</td>
</tr>
</table><!– end form-table –>
</div> <!– end #wet_container –>

<?php
// Enqueue the WordPress Media Uploader.
wp_enqueue_media();
}

add_action( ‘show_user_profile’, ‘wet_profile_img_fields’ );
add_action( ‘edit_user_profile’, ‘wet_profile_img_fields’ );

/**
* Save the new user CUPP url.
*
* @param int $user_id ID of the user’s profile being saved.
*/
function wet_save_img_meta( $user_id ) {
if ( ! current_user_can( ‘upload_files’, $user_id ) ) {
return;
}

$values = array(
// String value. Empty in this case.
‘wet_meta’ => filter_input( INPUT_POST, ‘wet_meta’, FILTER_SANITIZE_STRING ),

‘wet_upload_meta’ => filter_input( INPUT_POST, ‘wet_upload_meta’, FILTER_SANITIZE_URL ),

‘wet_upload_edit_meta’ => filter_input( INPUT_POST, ‘wet_upload_edit_meta’, FILTER_SANITIZE_URL ),
);

foreach ( $values as $key => $value ) {
update_user_meta( $user_id, $key, $value );
}
}

add_action( ‘personal_options_update’, ‘wet_save_img_meta’ );
add_action( ‘edit_user_profile_update’, ‘wet_save_img_meta’ );

/**
* Retrieve the appropriate image size
*
* @param int $user_id Default: $post->post_author. Will accept any valid user ID passed into this parameter.
* @param string $size Default: ‘thumbnail’. Accepts all default WordPress sizes and any custom sizes made by
* the add_image_size() function.
*
* @return string (Url) Use this inside the src attribute of an image tag or where you need to call the image url.
*/
function get_wet_meta( $user_id, $size = ‘thumbnail’ ) {
global $post;

if ( ! $user_id || ! is_numeric( $user_id ) ) {

$user_id = $post->post_author;
}
$baseurlim = esc_url( plugins_url( ‘user-profile-photo/img/placeholder.gif’ ) );
// Finally, check for image from an external URL. If none exists, return an empty string.
$attachment_ext_url = esc_url( get_the_author_meta( ‘wet_meta’, $user_id ) );

return $attachment_ext_url ? $attachment_ext_url : $baseurlim;
}

 

/**
* WordPress Avatar Filter
*
*/
function wet_avatar( $avatar, $identifier, $size, $alt ) {
if ( $user = wet_get_user_by_id_or_email( $identifier ) ) {
if ( $custom_avatar = get_wet_meta( $user->ID, ‘thumbnail’ ) ) {
return “<img alt='{$alt}’ src='{$custom_avatar}’ class=’avatar avatar-{$size} photo’ height='{$size}’ width='{$size}’ />”;
}
}

return $avatar;
}

add_filter( ‘get_avatar’, ‘wet_avatar’, 1, 5 );

/**
* Get a WordPress User by ID or email
*
* @param int|object|string $identifier User object, ID or email address.
*
* @return WP_User
*/
function wet_get_user_by_id_or_email( $identifier ) {
// If an integer is passed.
if ( is_numeric( $identifier ) ) {
return get_user_by( ‘id’, (int) $identifier );
}

// If the WP_User object is passed.
if ( is_object( $identifier ) && property_exists( $identifier, ‘ID’ ) ) {
return get_user_by( ‘id’, (int) $identifier->ID );
}

// If the WP_Comment object is passed.
if ( is_object( $identifier ) && property_exists( $identifier, ‘user_id’ ) ) {
return get_user_by( ‘id’, (int) $identifier->user_id );
}

return get_user_by( ’email’, $identifier );
}

function wooc_extra_register_fields() {
?>
<p class=”form-row form-row-first”>
<label for=”reg_billing_first_name”><?php _e( ‘First name’, ‘woocommerce’ ); ?> <span class=”required”>*</span></label>
<input type=”text” class=”input-text” name=”billing_first_name” id=”reg_billing_first_name” value=”<?php if ( ! empty( $_POST[‘billing_first_name’] ) ) esc_attr_e( $_POST[‘billing_first_name’] ); ?>” />
</p>

<p class=”form-row form-row-last”>
<label for=”reg_billing_last_name”><?php _e( ‘Last name’, ‘woocommerce’ ); ?> <span class=”required”>*</span></label>
<input type=”text” class=”input-text” name=”billing_last_name” id=”reg_billing_last_name” value=”<?php if ( ! empty( $_POST[‘billing_last_name’] ) ) esc_attr_e( $_POST[‘billing_last_name’] ); ?>” />
</p>

<div class=”clear”></div>

<p class=”form-row form-row-wide”>
<label for=”reg_billing_phone”><?php _e( ‘Profile Image URL’, ‘woocommerce’ ); ?> </label>
<input type=”text” class=”input-text” name=”wet_upload_meta” id=”reg_billing_phone” value=”<?php if ( ! empty( $_POST[‘wet_upload_meta’] ) ) esc_attr_e( $_POST[‘wet_upload_meta’] ); ?>” />
</p>

<?php
}
add_action( ‘woocommerce_register_form_start’, ‘wooc_extra_register_fields’ );

// 2. VALIDATE FIELDS

add_filter( ‘woocommerce_registration_errors’, ‘bbloomer_validate_name_fields’, 10, 3 );

function bbloomer_validate_name_fields( $errors, $username, $email ) {
if ( isset( $_POST[‘billing_first_name’] ) && empty( $_POST[‘billing_first_name’] ) ) {
$errors->add( ‘billing_first_name_error’, __( ‘<strong>Error</strong>: First name is required!’, ‘woocommerce’ ) );
}
if ( isset( $_POST[‘billing_last_name’] ) && empty( $_POST[‘billing_last_name’] ) ) {
$errors->add( ‘billing_last_name_error’, __( ‘<strong>Error</strong>: Last name is required!.’, ‘woocommerce’ ) );
}
return $errors;
}

function wooc_save_extra_register_fields( $customer_id ) {

if ( isset( $_POST[‘billing_first_name’] ) ) {
update_user_meta( $customer_id, ‘billing_first_name’, sanitize_text_field( $_POST[‘billing_first_name’] ) );
}
if ( isset( $_POST[‘billing_last_name’] ) ) {
update_user_meta( $customer_id, ‘billing_last_name’, sanitize_text_field( $_POST[‘billing_last_name’] ) );
}

if ( isset( $_POST[‘wet_upload_meta’] ) ) {
// WooCommerce billing phone
update_user_meta( $customer_id, ‘wet_upload_meta’, $_POST[‘wet_upload_meta’] );
update_user_meta( $customer_id, ‘wet_meta’, $_POST[‘wet_upload_meta’] );

}
}
add_action( ‘woocommerce_created_customer’, ‘wooc_save_extra_register_fields’ );

?>

Related Article


0 thoughts on “How to add user profile image by custom URL and custom fields in woocommerce”

Leave a Reply

Latest Article

Blog

Samsung Galaxy S8 might get a Portrait Mode with the next update

One of the hottest cell phone camera includes this year has seemingly been the Portrait mode, which enables a client to obscure the foundation of a subject along these lines giving more profundity to the Read more…

Blog

Huawei Mate 10 Lite with 18:9 display and four cameras declare

Huawei declare its leaders Mate 10 and Mate 10 Pro. In any case, in an unexpected move, Huawei has now quietly declared the spending gadget. The Huawei Mate 10 Lite too accompanies a stretched 5.9-inch Read more…

Blog

Xiaomi becomes India’s fastest growing smartphone brand

Riding on the accomplishment of its Redmi Note 4 gadget, Xiaomi has turned into the quickest developing cell phone mark in the second from last quarter of 2017, with a development of 292 for every Read more…