1

I'm trying to get the post thumbnail to change if a user selects a new file on a front end post edit screen. This is similar to the code I use to upload data and set the post thumbnail on a front end add new post form:

<?php
$query = new WP_Query( array( 'post_type' => 'properties', 'posts_per_page' => '-1' ) );

if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
  if( isset($_GET['post']) && $_GET['post'] == $post->ID) {         
    $current_post = $post->ID;
    $content = get_the_content();
    $price = get_post_meta($post->ID, 'shru_price', true);
    $address = get_post_meta($post->ID, 'shru_address', true);
    $thumbid = get_post_thumbnail_id( $post->ID );
    $thumbnail = wp_get_attachment_image_src( $thumbid, 'single-image' );
  }
endwhile; endif;
wp_reset_query();

global $current_post;

$postContentError = '';

if (
  isset( $_POST['submitted'] )
  && isset( $_POST['post_nonce_field'] )
  && wp_verify_nonce( $_POST['post_nonce_field'], 'post_nonce' )
) {
  if ( trim( $_POST['postContent'] ) === '' ) {
    $postContentError = 'Please enter a description of this property.';
    $hasError = true;
  }
  $post_information = array(
    'ID' => $current_post,
    'post_content' => $_POST['postContent'],
    'post_type' => 'properties',
    'post_status' => 'publish'
  );
  $post_id = wp_update_post( $post_information );

  function upload_user_file( $file = array() ) {
    global $post_id;        
    require_once( ABSPATH . 'wp-admin/includes/admin.php' );
    $file_return = wp_handle_upload( $file, array('test_form' => false ) );
    if( isset($file_return['error']) || isset($file_return['upload_error_handler']) ) {
      return false;
    } else {
      $filename = $file_return['file'];
      $attachment = array(
        'post_mime_type' => $file_return['type'],
        'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
        'post_content' => '',
        'post_status' => 'inherit',
        'guid' => $file_return['url']
      );
      $attachment_id = wp_insert_attachment($attachment, $file_return['url'], $post_id );
      require_once(ABSPATH . 'wp-admin/includes/image.php');
      $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename );
      wp_update_attachment_metadata( $attachment_id, $attachment_data );
      if( 0 < intval( $attachment_id ) ) {
        return $attachment_id;
      }
    }
    return false;
  }

  if( ! empty( $_FILES ) ) {
    foreach( $_FILES as $file ) {
      if( is_array( $file ) ) {
        $attachment_id = upload_user_file( $file );
      }
    }
  }

  $propertyfor = $_POST['propertyfor'];
  $propertytype = $_POST['propertytype'];
  $bedrooms = $_POST['bedrooms'];

  if( $post_id ) {

    // Update Custom Meta
    $price = esc_attr( strip_tags( $_POST['shru_price'] ) );
    update_post_meta( $post_id, 'shru_price', $price);
    $address = esc_attr( strip_tags( $_POST['shru_address'] ) ); 
    update_post_meta( $post_id, 'shru_address', $address );
    update_post_meta ($post_id, '_thumbnail_id', $attachment_id );

    // Update taxonomies        
    wp_set_object_terms( $post_id, $propertyfor, 'propertyfor' );
    wp_set_object_terms( $post_id, $propertytype, 'propertytype' );
    wp_set_object_terms( $post_id, $bedrooms, 'bedrooms' );

    // Redirect
    wp_redirect(home_url('/listings'));
    exit;
  } 
}
?>

The only difference is that in the code above I am trying to use:

update_post_meta( $post_id, '_thumbnail_id', $attachment_id );

instead of:

set_post_thumbnail( $post_id, $attachment_id );

For some reason, on the post edit screen the image file does not even upload. When I use update post meta, it removes the old thumbnail, so I guess it's doing it's job there, but since the file isn't uploading it cannot replace it with a new one. The confusion is why the file uploads using the upload_user_file function on the add new post screen but not the edit post screen.

Any ideas?

  • @s_ha_dum you able to shed any light on this? – CoreyRS Apr 8 '14 at 12:19
  • This is not a proper answer for your question. I just propose to use advanced custom fields plugin. It has front end form functionality - advancedcustomfields.com/resources/how-to/… I think this can save your time Best, Arthur – NetWeight Company Apr 10 '14 at 2:22
  • Do you have debugging information? – s_ha_dum Apr 10 '14 at 3:38
  • @s_ha_dum nothing that seems relevant to this issue. – CoreyRS Apr 10 '14 at 4:05
  • @G. M. not sure of the purpose of your edit. can you explain? – CoreyRS Apr 10 '14 at 6:44
3
+100

it is much easier with WordPress inbuilt function media_handle_upload

http://codex.wordpress.org/Function_Reference/media_handle_upload

// These files need to be included as dependencies when on the front end.
require_once( ABSPATH . 'wp-admin/includes/image.php' );
require_once( ABSPATH . 'wp-admin/includes/file.php' );
require_once( ABSPATH . 'wp-admin/includes/media.php' );

// Let WordPress handle the upload.
// Remember, 'my_image_upload' is the name of our file input in our form above.
$attachment_id = media_handle_upload( 'my_image_upload', $_POST['post_id'] );

if ( is_wp_error( $attachment_id ) ) {
    // There was an error uploading the image.
} else {
    // The image was uploaded successfully!
}

You will need to specify your file control name, then you can call set_post_thumbnail function OR set post meta '_thumbnail_id'

set_post_thumbnail( $post_id, $attachment_id );

EDIT:

For some reason, on the post edit screen the image file does not even upload.

Can you double check that you have properly set enctype="multipart/form-data" attribute to your form tag?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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