Thursday, 5 September 2013

Getting the attachment URL instead of the ID (Wordpress)

Getting the attachment URL instead of the ID (Wordpress)

I have a script that allows me to upload an image to wordpress from the
front end. I then need it to post the file to the post_meta. Right now
it's working fine, BUT I end up with the Attachment ID and need the LINK
to the file.
Here's the code that is handling this particular function.
if( $_FILES ){
foreach ($_FILES as $k=>$v){
if( $k!='poster_has_paid' && $k!='featured_image'){
if($_FILES[$k]){
wpo_poster_insert_attachment($k, $post_id,
false, $k);
}
}
}
}
And here is the function wpo_poster_insert_attachment
function
wpo_poster_insert_attachment($file_handler,$post_id,$setthumb='false',
$post_meta = '') {
// check to make sure its a successful upload
if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) __return_false();
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
$attach_id = media_handle_upload( $file_handler, $post_id );
if ($setthumb) update_post_meta($post_id,'_thumbnail_id',$attach_id);
if(!$setthumb && $post_meta!=''){
update_post_meta($post_id, $post_meta, $attach_id);
}
return $attach_id;
Again, it's updating the field with the attach_id, and I'd like it to
update the attach_url
PS I will give thanks when I have enough posts to do so. Thanks in advance.

No comments:

Post a Comment