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 14 years ago on Friday December 17, 2010 |
Permalink