Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have various different extra profile fields which I have set in functions.php however with the upload one if a user uploads an image and saves the form the image is set, however if they go back and edit another field it sets the image field to null.

Here is the code I have in my functions.php file. Any help would be really appreciated.

     <?php

add_action( 'show_user_profile', 'extra_user_profile_fields' );
add_action( 'edit_user_profile', 'extra_user_profile_fields' );

function extra_user_profile_fields( $user ) { 

$r = get_user_meta( $user->ID, 'picture', true );
    ?>


<!-- Artist Photo Gallery -->
<h3><?php _e("Public Profile - Gallery", "blank"); ?></h3>

<table class="form-table">

<tr>
        <th scope="row">Picture</th>
        <td><input type="file" name="picture" value="" />

            <?php //print_r($r); 
                if (!isset($r['error'])) {
                    $r = $r['url'];
                    echo "<img src='$r' />";
                } else {
                    $r = $r['error'];
                    echo $r;
                }
            ?>
        </td>
    </tr>

</table> 



<?php
}

add_action( 'personal_options_update', 'save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_user_profile_fields' );

function save_extra_user_profile_fields( $user_id ) {

if ( !current_user_can( 'edit_user', $user_id ) ) { return false; }

$_POST['action'] = 'wp_handle_upload';
$r = wp_handle_upload( $_FILES['picture'] );
update_user_meta( $user_id, 'picture', $r, get_user_meta( $user_id, 'picture', true ) );

}

}

add_action('user_edit_form_tag', 'make_form_accept_uploads');
function make_form_accept_uploads() {
    echo ' enctype="multipart/form-data"';
}
share|improve this question
    
For one: Why is there markup in your functions.php? Move all markup to a template file. Also, you could save a ton of code by defining the fields in an array and echoing markup only while iterating over said array of profile fields. As for your actual question, the overwritten picture / picture's URL: Be so kind as to limit the code you post to relevant sections. Thanks. –  Johannes Pille Jun 2 '12 at 15:12
    
@JohannesPille Hi, I've updated the code to only include the upload field. –  Storm3y Jun 2 '12 at 15:48
add comment

1 Answer

up vote 1 down vote accepted

The last parameter of update_user_meta(), the previous value, is an optional parameter. If it's set it checks whether the value in the database is indeed the one you fed update_user_meta(). If you set that paramteter by grabbing the value from the database, it is completely redundant. So first off, omit that.

That being said, this is what solves your problem with overwriting:

if( $_FILES['picture']['error'] === UPLOAD_ERR_OK ) {
    $r = wp_handle_upload( $_FILES['picture'] );
    update_user_meta( $user_id, 'picture', $r );
}

The terminology is a bit confusing, since UPLOAD_ERR_OK is a status message and not an error, but that's how to check whether an upload was successful. If you make that the condition for saving the meta value, you're good to go.

For further reference on the $_FILES superglobal's errors or statuses, see Error Messages Explained from the PHP manual.

EDIT: How to get the URL of the uploaded image

This edit caters to the expanded question in the comment to this answer.

$pic_data = get_user_meta( $curauth->ID, 'picture', true );
$pic_url = $pic_data['url'];

will save the URL into a variable, which thereupon can be echoed wherever you want. Assuming that $curauth is the current user object. You could use the global WordPress variable $current_user instead, but if you have the object already, might as well go with that.

share|improve this answer
    
Works brilliantly :) My other question if you know, how would I get the URL of the uploaded image on the users author.php page? For the other fields I used <?php echo $curauth->twitter; ?> but that doesn't appear to work with the file upload field. –  Storm3y Jun 2 '12 at 18:01
    
@Storm3y, see the edit above. –  Johannes Pille Jun 2 '12 at 18:09
    
Still doesn't appear to work, it seems there is various amount of data stored in the database and not just the URL to that image. –  Storm3y Jun 2 '12 at 18:17
    
Sorry, that was incomplete. The return value of wp_handle_upload is an associative array of file attributes and that's how you save it to the DB. Hence, when you retrieve it, you also get the array back. The second line in the edit gives you the URL only. –  Johannes Pille Jun 2 '12 at 18:27
    
Works perfectly :) Thank you. –  Storm3y Jun 2 '12 at 18:32
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.