Search
 
SCRIPT & CODE EXAMPLE
 

PHP

woocommerce profile photo upload

// 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' );
Comment

PREVIOUS NEXT
Code Example
Php :: laravel disable csrf token 
Php :: why pusher not working in laravel 
Php :: laravel deploy without moving public directory 
Php :: laravel query latest 
Php :: laravel ckeditor 
Php :: php sort associative array by specific value 
Php :: php get country from cloudflare 
Php :: laravel asset 
Php :: cake php 2.x joins 
Php :: php mysql search database and display results 
Php :: php round to whole number 
Php :: php foreach random 
Php :: delete mysql php 
Php :: get_the_author_meta display name 
Php :: store image in storage laravel 
Php :: php uppercase first letter 
Php :: php check if folder exists 
Php :: PHP file reading modes with explaination 
Php :: brew php version 
Php :: how to use plugin shortcode in wordpress template 
Php :: php auto redirect 
Php :: session laravel 
Php :: php check session status 
Php :: how to add sidebar to page.php 
Php :: php unset reference 
Php :: PHP validation/regex for URL 
Php :: window.location javascript php 
Php :: cc in wp_mail 
Php :: php recursive function to build array 
Php :: laravel eloquent mass update 
ADD CONTENT
Topic
Content
Source link
Name
5+2 =