Skip to main content
If you don’t pass 'test_form' => FALSE in the wp_handle_upload() then the upload will be rejected
Source Link

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.

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.

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.

deleted 2 characters in body
Source Link
Johannes Pille
  • 11.1k
  • 3
  • 42
  • 53

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_author$current_user instead, but if you have the object already, might as well go with that.

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_author instead, but if you have the object already, might as well go with that.

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.

added 35 characters in body
Source Link
Johannes Pille
  • 11.1k
  • 3
  • 42
  • 53

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_url$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_author instead, but if you have the object already, might as well go with that.

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_url = get_user_meta( $curauth->ID, 'picture', true );

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_author instead, but if you have the object already, might as well go with that.

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_author instead, but if you have the object already, might as well go with that.

added 471 characters in body
Source Link
Johannes Pille
  • 11.1k
  • 3
  • 42
  • 53
Loading
Source Link
Johannes Pille
  • 11.1k
  • 3
  • 42
  • 53
Loading