PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Upload Image

  1. Howdy,

    I'm stuck working with the images from a form sent to a custom post. What I need to do is retrieve the images so they can be over-written. Gravity forms attaches the image(s) to the post, but maybe I'm just not finding the right resources to be able to delete those uploaded files via form/custom coding.

    resources via google aren't very helpful, or my searching simply sucks here...

    As a helpful bonus, I've attached a nifty script I wrote to cycle through the custom data in a post (that I've put via GF post data) and update it accordingly (through a custom form, as GF doesn't easily support this yet.)

    Note the only real ability missing is uploading new images and removing the old one. See problem above.

    Update loop:
    http://www.pastie.org/1372528

    p.s. feel free to improve on that script if anyone finds a use other then myself. It's a first draft.

    Posted 13 years ago on Monday December 13, 2010 | Permalink
  2. @urmedia i'm not entirely clear what you are trying to do. You are trying to delete the images that are uploaded? Can you provide more detail on what you are trying to do and how the workflow is supposed to work? Why are the images going to be over-written?

    Posted 13 years ago on Monday December 13, 2010 | Permalink
  3. ah. Sorry.

    I need to upload an image (similar to the file) and be able to check if an image exists. If the image does exist, I need it deleted. Basically to only keep a single image attached to each post instead of appending more every time the upload image is submitted.

    Workflow:

    I use a GF form to establish the initial post creation with heavy use of custom fields. I use a single file upload field and image upload field. These are all associated with the post created.

    GF doesn't currently work with data already created very easily so I've recreated the GF form and populated it with the post_meta. This is done so the old values can be seen before editing (clicking submit) with new values.

    However, Files are assigned by link in a custom field. When an image is uploaded via GF it is created as an attachement 'image'.

    As you can see in the base script in my post, the file feature is checked and, if an old file is found it deletes it from the server - all the while populating with the new file. I need to be able to do the same thing with images (and all sub images created (thumb, medium, large,original)).

    I can't figure out how to upload a new image to a post while deleting the old one (if one exists).

    Am I missing a built-in wp_function that can do this?

    Posted 13 years ago on Monday December 13, 2010 | Permalink
  4. Ok. Seems that there is no answer from your team.

    I was hoping to learn what GF uses to hook in to the wordpress attachement for images as I could then use that to run a comparison and then delete the attachements.

    Posted 13 years ago on Thursday December 16, 2010 | Permalink
  5. We haven't implemented anything like this before. When we created the functionality we only looked into how to create the post data, not how to edit it. So i'm not entirely sure what built in WP functions you would use to edit the media library data for an existing post before then adding a new one. It's slightly different editing than creating a post.

    Posted 13 years ago on Thursday December 16, 2010 | Permalink
  6. Ok. How bout this...

    the following deletes the old image (handled from a form) and the next step is to upload the new one.

    //if image field has data
    	 if(!empty($_FILES))
    	 {
    	  $filename = $_FILES['image']['name'];
    	  echo $filename;
    	  if($images = get_children(array(
    		'post_parent'    => get_the_ID(),
    		'post_type'      => 'attachment',
    		'numberposts'    => 1, // show one
    		'post_status'    => null,
    		'post_mime_type' => 'image',
    	     )))
    	  {
    		foreach($images as $image) {
    		$img_ID = $image->ID;
    		break;
    		}
    	  }
    	 wp_delete_attachment( $img_ID, $force_delete = true ); //delete old images

    How can I hook into WP or GF to upload a new one? --Keep in mind that GF upload image already does this somehow...it's the 'How I am looking for.

    by the way - I couldn't work a smaller approach to getting the attachment ID then running a foreach.

    Posted 13 years ago on Friday December 17, 2010 | Permalink
  7. c'mon, guys. I figured I'd trade code for code. Is that not a fair deal, here?

    Posted 13 years ago on Friday December 17, 2010 | Permalink
  8. Nevermind. I found the solution in a rather obscure place that has nothing to do with GF - the wordpress core media.php.

    Pastie is down and my antvirus hates the other link so don't you yell at me!

    I'm terrible at writing plugins, so maybe someone else can make a profit from this. (like the guys at GF, perhaps..)

    Kevin, you always have technical and knowledgable answers so feel free to ask me questions. This is a section of code (fully functional) which deletes the old post image and adds the newly uploaded one. I've added a file path variable for file handling of documents related to the post for the extended script.

    As you are aware I'm building a jobs based website so, essentially a mild form of a membership site. Uploading and downloading profile/company images is a good feature to have, as well as resumes. It would be very easy to allow modification for a user based document sharing site, or many other ideas.

    //If we submitted the form
    if(isset($_POST['submit']))
    {
         array_pop($_POST); //remove the submit button as post data from array
    	 $post_id = $_POST['post_id']; //get the id of the post
    	 unset($_POST['post_id']); //lets not add the hidden post_id field
    	 //the directory path to upload the files to
    	 $uploaddir = ABSPATH . 'wp-content/uploads/post-' . $post_id . '/';
    
    	 //if image field has data
    	 if(!empty($_FILES))
    	 {
    	  $filename = $_FILES['image']['name'];
    
    	  wp_mkdir_p( $uploaddir );//check if the directory exists and create one if it doesn't
    
    	//delete attached image
    	  if($images = get_children(array(
    		'post_parent'    => $post_id,
    		'post_type'      => 'attachment',
    		'numberposts'    => 1, // show one
    		'post_status'    => null,
    		'post_mime_type' => 'image',
    	     )))
    	  {
    		foreach($images as $image) {
    		$img_ID = $image->ID;
    		break;
    		}
    	  }
    	 wp_delete_attachment( $img_ID, $force_delete = false ); //delete old images
    
    	 //Upload new image
    	 require_once(ABSPATH . 'wp-admin/includes/admin.php');  //include this !IMPORTANT
         media_handle_upload('async-upload', $post_id); //use WP media handler to upload the file 
    
    	}
    
    	 //update the fields
    	 foreach($_POST as $key => $value)
    	 {
    
             update_post_meta($post_id, $key, $value);
    
         }
    }
    Posted 13 years ago on Friday December 17, 2010 | Permalink
  9. @urmedia

    We'll take a look at your code snippet and offer some feedback as soon as we can.

    "c'mon, guys. I figured I'd trade code for code. Is that not a fair deal, here?"

    As far as the quoted post, you posted the previous question at what was almost 9pm our time zone. Then, the subsequent post would have been around 4:30am or so our time.

    The admins on the site are generally available from 9am to 6pm EST and we try to answer everything as quickly as we can during that time period. After hours, we try to monitor the forums as we can, but we can't and don't guarantee that we'll be available to answer every forum question within such a short time frame. You simply have to be more patient and know that we'll make every effort we can to reply in a timely manner with a well-researched and knowledgeable answer.

    That said, I'd also ask that you tone down the back-handed remarks and snubs pointed at the developers. Since you've joined the forums, this has been a consistent trend in your posts. I'm not sure if this is intentional, but If you want us to help you out, that's not the way to motivate us to do so.

    Sure, we're a small company and the product/documentation isn't perfect. It simply never will be but we're constantly evolving & improving every aspect of the product & services around it.

    You're obviously a knowledgeable developer and have plenty of information that could benefit the community here. We really appreciate that and welcome that kind of participation. Please try to place nice and we'll all have a great relationship moving forward.

    Posted 13 years ago on Friday December 17, 2010 | Permalink
  10. Yep.
    "c'mon, guys. I figured I'd trade code for code. Is that not a fair deal, here?"

    It's true that humor doesn't always translate well in text format.

    Frankly, one of the developers left a sour taste shortly after joining/buying the product and I've been trying to forget about it. It's that whole 'first impression' thing and the rudeness lingers in the back of my mind and reflects in my writing.

    For the reflection in my writing I do apologize and will make attempts to edit a wee bit better.

    Regarding the rude first impression..it may well have been an interpretation akin to the one you took with the
    "c'mon, guys. I figured I'd trade code for code. Is that not a fair deal, here?"

    A simple miscommunication.

    Posted 13 years ago on Friday December 17, 2010 | Permalink
  11. @urmedia

    Thanks for the response and the clarification. I do understand that things can be misconstrued and we all come across differently than we intend from time to time. I apologize if we've offended you along the line, but please know it wasn't intended.

    As I said earlier, we value your input and appreciate your willingness to jump in there and help folks here in the forums.. it goes towards building a better community and ultimately a better product for everyone.

    Posted 13 years ago on Friday December 17, 2010 | Permalink