Edit User Profile From Front End

I have taken reference from this link. I have used Plugin
cimy user extra fields
for extra users’ field. I have taken few extra fields such as picture upload and file upload.

Now I want to edit user profile from front end. I am able to edit user text fields(both extra and normal fields), but I am not able to change their picture or file.

Read More

I have also tried to use few plugins such as profile builder, wp user frontend but have not been successful in doing so. Below is my code which i have used for editing user profile from front

Form

 <form method="post" id="your-profile" action="<?php the_permalink(); ?>"  enctype="multipart/form-data">
                                <table>
                                    <tr>
                                        <td><label for="first-name"><?php _e('First Name', 'profile'); ?></label></td>
                                        <td><input class="text-input" name="first-name" type="text" id="first-name" value="<?php the_author_meta('first_name', $current_user->ID); ?>" /></td>
                                    </tr>
                                    <tr>
                                        <td><label for="last-name"><?php _e('Last Name', 'profile'); ?></label></td>
                                        <td><input class="text-input" name="last-name" type="text" id="last-name" value="<?php the_author_meta('last_name', $current_user->ID); ?>" /></td>
                                    </tr>
                                    <tr>
                                        <td><label for="email"><?php _e('Email', 'profile'); ?></label></td>
                                        <td><input class="text-input" name="email" type="text" id="email" value="<?php the_author_meta('user_email', $current_user->ID); ?>" /></td>
                                    </tr>
                                    <tr>
                                        <td><label for="url"><?php _e('Website', 'profile'); ?></label></td>
                                        <td><input class="text-input" name="url" type="text" id="url" value="<?php the_author_meta('user_url', $current_user->ID); ?>" /></td>
                                    </tr>
                                    <tr>
                                        <td><label for="pass1"><?php _e('Password *', 'profile'); ?> </label></td>
                                        <td><input class="text-input" name="pass1" type="password" id="pass1" /></td>
                                    </tr>
                                    <tr>
                                        <td><label for="pass2"><?php _e('Repeat Password *', 'profile'); ?></label></td>
                                        <td><input class="text-input" name="pass2" type="password" id="pass2" /></td>
                                    </tr>
                                    <tr>
                                        <td><label for="description"><?php _e('Biographical Information', 'profile') ?></label></td>
                                        <td><textarea name="description" id="description" rows="3" cols="50"><?php the_author_meta('description', $current_user->ID); ?></textarea></td>
                                    </tr>
                                </table>
                                <?php wp_nonce_field('update_user','update_user_nonce'); ?>

                                <?php
                                //action hook for plugin and extra fields
                                do_action('edit_user_profile', $current_user);
                                ?>
                                <p class="form-submit">
                                    <input type="submit" id="updateuser" class="submit button" value="<?php _e('Update', 'profile'); ?>" />
                                </p>
                            </form>

Code to update profile

        global $current_user;

       get_currentuserinfo();

       /* Load the registration file. */
       if (!(function_exists('get_user_to_edit'))) {
           require_once(ABSPATH . '/wp-includes/user.php');
           require_once(ABSPATH . '/wp-admin/includes/user.php');
       }

       $allFields = get_cimyFields();
       $error = array();

       /* If profile was saved, update profile. */
       if ('POST' == $_SERVER['REQUEST_METHOD'] &&                       !empty($_REQUEST['update_user_nonce']) && wp_verify_nonce($_REQUEST['update_user_nonce'],'update_user') ) {

/* Update user password. */
if (!empty($_POST['pass1']) && !empty($_POST['pass2'])) {
    if ($_POST['pass1'] == $_POST['pass2'])
        wp_update_user(array('ID' => $current_user->ID, 'user_pass' => esc_attr($_POST['pass1'])));
    else
        $error[] = __('The passwords you entered do not match.  Your password was not updated.', 'profile');
}


/* Update user information. */
if (!empty($_POST['url'])) {
    wp_update_user(array('ID' => $current_user->ID, 'user_url' => esc_url($_POST['url'])));
}

if (!empty($_POST['email'])) {
    if (!is_email(esc_attr($_POST['email'])))
        $error[] = __('The Email you entered is not valid.  please try again.', 'profile');
    elseif (email_exists(esc_attr($_POST['email'])) != $current_user->ID)
        $error[] = __('This email is already used by another user.  try a different one.', 'profile');
    else {
        wp_update_user(array('ID' => $current_user->ID, 'user_email' => esc_attr($_POST['email'])));
    }
}

if (!empty($_POST['first-name'])) {
    update_user_meta($current_user->ID, 'first_name', esc_attr($_POST['first-name']));
}

if (!empty($_POST['last-name'])) {
    update_user_meta($current_user->ID, 'last_name', esc_attr($_POST['last-name']));
}

if (!empty($_POST['description'])) {
    update_user_meta($current_user->ID, 'description', esc_attr($_POST['description']));
}


/* Redirect so the page will show updated info. */
/* I am not Author of this Code- i dont know why but it worked for me after changing below line to if ( count($error) == 0 ){ */
if (count($error) == 0) {

    //action hook for plugins and extra fields saving
    do_action('personal_options_update', $current_user->ID);
    do_action('profile_update', $current_user->ID);
    foreach ($allFields as $field) {
        set_cimyFieldValue($current_user->ID, $field["NAME"], $_POST['cimy_uef_' . $field['NAME']]);
    }
    do_action('edit_user_profile_update', $current_user->ID);
    wp_redirect(get_permalink());
       //        exit;
}
       }
       add_action('wp_enqueue_scripts', 'abc');

       function abc() {
           wp_enqueue_script('image-edit');
           wp_enqueue_style('imgareaselect');
       }

Related posts