Frontend Image Upload

I have the following code used at the top of my front end user profile page, everything works and displays fine except for the following problem ….

If I upload an image and submit the form usermeta ‘profile-image’ updates correctly, however if I update the form without uploading this meta is deleted and looks like the update_usermeta call is still firing and making empty.

Read More

Not sure what I am doing quite wrong, driving me nuts

                <?php
            global $current_user, $wp_roles;
            get_currentuserinfo();

            require_once( ABSPATH . WPINC . '/registration.php' );
            $error = array();    

            if ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == 'update-user' ) {

                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_usermeta( $current_user->ID, $_POST['country'] );
                update_usermeta( $current_user->ID, $_POST['identity'] );
                update_usermeta( $current_user->ID, $_POST['workplace'] );
                update_usermeta( $current_user->ID, $_POST['public_email'] );

                $bio_summary = sanitize_text_field($_POST['bio-summary']);
                update_usermeta( $current_user->ID, 'bio_summary', $bio_summary );

                $web_link = esc_url_raw($_POST['web-link'], array('http', 'https'));
                update_usermeta( $current_user->ID, 'web_link', $web_link );

                $bio_longer = sanitize_text_field($_POST['bio-longer']);
                update_usermeta( $current_user->ID, 'bio_longer', $bio_longer );

                if ( count($error) == 0 ) {
                    do_action('edit_user_profile_update', $current_user->ID);
                    //wp_redirect( get_permalink() );
                }
            }                   
            ?>
            <?php 
            if ($_FILES['profile-image']['error'] ==0 ){
                $attach_id = ap_apply_online_image_handler();
                $attach_url = wp_get_attachment_url( $attach_id );
                update_usermeta( $current_user->ID, 'profile_image', $attach_url );
            } else {
                echo 'Upload error';
            }
            ?>

This the form ….

                                <form action="" method="post" enctype="multipart/form-data" >
                                <table class="form-table">
                                    <tr>
                                        <th><label for="profile-image">Profile Image:</label></th>        
                                        <td>
                                            <input type="file" id="profile-image" name="profile-image" />
                                            <input type="text" id="profile-image-set" name="profile-image-set" value="<?php echo esc_url( get_the_author_meta( 'profile_image', $current_user->ID ) ); ?>" class="regular-text" />
                                        </td>
                                    </tr>
                                    <tr>
                                        <th><label for="country">Country</label></th>
                                        <td>
                                        <?php $country = get_the_author_meta( 'country', $current_user->ID ); ?>
                                        <select name="country" id="country">
                                            <option value="not specified" <?php echo ($country == "not specified")?  'selected="selected"' : '' ?>>Not Specified</option>
                                            <option value="australia" <?php echo ($country == "australia")?  'selected="selected"' : '' ?>>Australia</option>
                                            <option value="bangladesh" <?php echo ($country == "bangladesh")?  'selected="selected"' : '' ?>>Bangladesh</option>
                                            <option value="bhutan" <?php echo ($country == "bhutan")?  'selected="selected"' : '' ?>>Bhutan</option>
                                            <option value="brunei" <?php echo ($country == "brunei")?  'selected="selected"' : '' ?>>Brunei</option>
                                            <option value="cambodia" <?php echo ($country == "cambodia")?  'selected="selected"' : '' ?>>Cambodia</option>
                                            <option value="canada" <?php echo ($country == "canada")?  'selected="selected"' : '' ?>>Canada</option>
                                            <option value="china" <?php echo ($country == "china")?  'selected="selected"' : '' ?>>China</option>
                                            <option value="fiji" <?php echo ($country == "fiji")?  'selected="selected"' : '' ?>>Fiji</option>
                                            <option value="hong kong" <?php echo ($country == "hong kong")?  'selected="selected"' : '' ?>>Hong Kong</option>
                                            <option value="india" <?php echo ($country == "india")?  'selected="selected"' : '' ?>>India</option>
                                            <option value="indonesia" <?php echo ($country == "indonesia")?  'selected="selected"' : '' ?>>Indonesia</option>
                                            <option value="japan" <?php echo ($country == "japan")?  'selected="selected"' : '' ?>>Japan</option>
                                            <option value="korea" <?php echo ($country == "korea")?  'selected="selected"' : '' ?>>Korea</option>
                                            <option value="laos" <?php echo ($country == "laos")?  'selected="selected"' : '' ?>>Laos</option>
                                            <option value="malaysia" <?php echo ($country == "malaysia")?  'selected="selected"' : '' ?>>Malaysia</option>
                                            <option value="nepal" <?php echo ($country == "nepal")?  'selected="selected"' : '' ?>>Nepal</option>
                                            <option value="new zealand" <?php echo ($country == "new zealand")?  'selected="selected"' : '' ?>>New Zealand</option>
                                            <option value="myanmar" <?php echo ($country == "myanmar")?  'selected="selected"' : '' ?>>Myanmar</option>
                                            <option value="singapore" <?php echo ($country == "singapore")?  'selected="selected"' : '' ?>>Singapore</option>
                                            <option value="philippines" <?php echo ($country == "philippines")?  'selected="selected"' : '' ?>>Philippines</option>
                                            <option value="sri lanka" <?php echo ($country == "sri lanka")?  'selected="selected"' : '' ?>>Sri Lanka</option>
                                            <option value="taiwan" <?php echo ($country == "taiwan")?  'selected="selected"' : '' ?>>Taiwan</option>
                                            <option value="thailand" <?php echo ($country == "thailand")?  'selected="selected"' : '' ?>>Thailand</option>
                                            <option value="tibet" <?php echo ($country == "tibet")?  'selected="selected"' : '' ?>>Tibet</option>
                                            <option value="vietnam" <?php echo ($country == "vietnam")?  'selected="selected"' : '' ?>>Vietnam</option>
                                            <option value="united kingdom" <?php echo ($country == "united kingdom")?  'selected="selected"' : '' ?>>United Kingdom</option>
                                            <option value="united states" <?php echo ($country == "united states")?  'selected="selected"' : '' ?>>United States</option>
                                            <option value="other" <?php echo ($country == "other")?  'selected="selected"' : '' ?>>Other</option>
                                        </select>
                                        </td>
                                    </tr>
                                    <tr>
                                        <th><label for="identity">How do you identify yourself?:</label></th>
                                        <td>
                                        <?php $identity = get_the_author_meta( 'identity', $current_user->ID ); ?>
                                        <select name="identity" id="identity">
                                            <option value="not specified" <?php echo ($identity == "not specified")?  'selected="selected"' : '' ?>>Not Specified</option>
                                            <option value="writer" <?php echo ($identity == "writer")?  'selected="selected"' : '' ?>>Writer</option>
                                            <option value="translator" <?php echo ($identity == "translator")?  'selected="selected"' : '' ?>>Translator</option>
                                            <option value="publisher" <?php echo ($identity == "publisher")?  'selected="selected"' : '' ?>>Publisher</option>
                                            <option value="festival organiser" <?php echo ($identity == "festival organiser")?  'selected="selected"' : '' ?>>Festival Organiser</option>
                                            <option value="student" <?php echo ($identity == "student")?  'selected="selected"' : '' ?>>Student</option>
                                            <option value="literary agent" <?php echo ($identity == "literary agent")?  'selected="selected"' : '' ?>>Literary Agent</option>
                                            <option value="academic" <?php echo ($identity == "academic")?  'selected="selected"' : '' ?>>Academic</option>
                                            <option value="other" <?php echo ($identity == "other")?  'selected="selected"' : '' ?>>Other</option>
                                        </select>
                                        </td>
                                    </tr>
                                    <tr>
                                        <th><label for="workplace">Workplace:</label></th>

                                        <td>
                                        <?php $workplace = get_the_author_meta( 'workplace', $current_user->ID ); ?>
                                        <select name="workplace" id="workplace">
                                            <option value="not specified" <?php echo ($workplace == "not specified")?  'selected="selected"' : '' ?>>Not Specified</option>
                                            <option value="university" <?php echo ($workplace == "university")?  'selected="selected"' : '' ?>>University</option>
                                            <option value="school" <?php echo ($workplace == "school")?  'selected="selected"' : '' ?>>School</option>
                                            <option value="literary organisation" <?php echo ($workplace == "literary organisation")?  'selected="selected"' : '' ?>>Literary Organisation</option>
                                            <option value="writers circle" <?php echo ($workplace == "writers circle")?  'selected="selected"' : '' ?>>Writers' Circle</option>
                                            <option value="other" <?php echo ($workplace == "other")?  'selected="selected"' : '' ?>>Other</option>
                                        </select>
                                        </td>
                                    </tr>
                                    <tr>
                                        <th><label for="bio-summary">BIO Summary (circa 300 words):</label></th>
                                        <td>
                                        <textarea name="bio-summary" id="bio-summary" rows="5" cols="30"><?php echo esc_textarea( get_the_author_meta( 'bio_summary', $current_user->ID ) ); ?></textarea>
                                        </td>
                                    </tr>
                                    <tr>
                                        <th><label for="web-link">Your Web Link:</label></th>
                                        <td>
                                            <input type="text" name="web-link" id="web-link" value="<?php echo esc_url( get_the_author_meta( 'web_link', $current_user->ID ) ); ?>" class="regular-text" />
                                        </td>
                                    </tr>
                                    <tr>
                                        <th><label for="bio-longer">Longer BIO CV including publications <br/>(optional):</label></th>
                                        <td>
                                        <textarea name="bio-longer" id="bio-longer" rows="5" cols="30"><?php echo esc_textarea( get_the_author_meta( 'bio_longer', $current_user->ID ) ); ?></textarea>
                                        </td>
                                    </tr>
                                    <tr>
                                        <th><label for="public_email">Email Display:</label></th>
                                        <?php $public_email = get_the_author_meta( 'public_email', $current_user->ID ); ?>
                                        <td>
                                        <input type="checkbox" name="public_email" id="public_email" value="yes" <?php echo ($public_email == "yes") ? 'checked="checked"' : '' ?> /><span class="description"><?php _e('&nbsp;I agree to my email being publically displayed.') ?></span><br />
                                        </td>
                                    </tr>
                                </table>

                                <?php //do_action('edit_user_profile',$current_user); ?>

                                <p class="form-submit">
                                    <?php echo $referer; ?>
                                    <input name="updateuser" type="submit" id="updateuser" class="submit button" value="<?php _e('Update', 'profile'); ?>" />
                                    <?php wp_nonce_field( 'update-user' ) ?>
                                    <input name="action" type="hidden" id="action" value="update-user" />
                                </p><!-- .form-submit -->

                            </form><!-- #adduser -->

Thanks

John

Related posts