1

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"';
}
2
  • 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. Commented Jun 2, 2012 at 15:12
  • @JohannesPille Hi, I've updated the code to only include the upload field.
    – Storm3y
    Commented Jun 2, 2012 at 15:48

2 Answers 2

2

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 ) {
    $upload_overrides = array( 'test_form' => false ); // if you don’t pass 'test_form' => FALSE the upload will be rejected
    $r = wp_handle_upload( $_FILES['picture'], $upload_overrides );
    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.

4
  • 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
    Commented Jun 2, 2012 at 18:01
  • @Storm3y, see the edit above. Commented Jun 2, 2012 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
    Commented Jun 2, 2012 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. Commented Jun 2, 2012 at 18:27
0
// for file upload
add_action('user_edit_form_tag', 'make_form_accept_uploads');
function make_form_accept_uploads() {
    echo ' enctype="multipart/form-data"';
}

function custom_user_profile_fields($user)
{
    if(is_object($user)) {
        $r = get_user_meta( $user->ID, 'portfolioimage', true );
    } else {
        $r = null;
    }

    ?>
    ## Heading ##
    <table class="form-table">
        <h3>Portfolio</h3>
        <tr>
            <th><label for="image">Portfolio Image</label></th>
            <td>
                <?php 
                    if (!isset($r['error'])) {
                        $r = $r['url'];
                        echo "<img src='$r' width='96' hieght='96'/>";
                    } else {
                        $r = $r['error'];
                        echo $r;
                    }
                ?>
                <br/>
                <span class="description">Please upload an image for your profile.</span>
                <br/>
                <input type="file" class="button-primary" value="Upload Image" id="portfolioimage" name="portfolioimage" multiple/><br />
            </td>
        </tr>
    </table>    
    <?php
}

add_action( "show_user_profile", "custom_user_profile_fields" );
add_action( "edit_user_profile", "custom_user_profile_fields" );
add_action( "user_new_form", "custom_user_profile_fields" );
function save_custom_user_profile_fields($user_id)
{
# again do this only if you can
if(!current_user_can('manage_options'))
    return false;

  if( $_FILES['portfolioimage']['error'] === UPLOAD_ERR_OK ) 
  {
    $upload_overrides = array( 'test_form' => false ); 
    $r = wp_handle_upload( $_FILES['portfolioimage'], $upload_overrides );
    update_user_meta( $user_id, 'portfolioimage', $r );
  }
}

add_action('user_register', 'save_custom_user_profile_fields');
add_action('profile_update', 'save_custom_user_profile_fields');
3
  • U can also add multiple image using Commented Jan 11, 2018 at 6:08
  • <img src='$r' width='96' hieght='96' multiple/> Commented Jan 11, 2018 at 6:08
  • Please edit your answer, and add an explanation: why could that solve the problem?
    – fuxia
    Commented Jan 11, 2018 at 7:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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