Google help on software/programming And Windows Tricks: Image upload from frontend in wordpress

helponsoftware

Monday 27 April 2015

Image upload from frontend in wordpress

function insert_attachment() {
$uploaddir = wp_upload_dir();
$file = $_FILES['poster'];
$uploadfile = $uploaddir['path'] . '/' . basename( $file['name'] );

move_uploaded_file( $file['tmp_name'] , $uploadfile );
$filename = basename( $uploadfile );

$wp_filetype = wp_check_filetype(basename($filename), null );

$attachment = array(
    'post_mime_type' => $wp_filetype['type'],
    'post_title' => preg_replace('/\.[^.]+$/', '', $filename),
    'post_content' => '',
    'post_status' => 'inherit',
    'menu_order' => $_i + 1000
);
$attach_id = wp_insert_attachment( $attachment, $uploadfile );
$fullsizepath = get_attached_file( $attach_id );
$metadata = wp_generate_attachment_metadata( $attach_id, $fullsizepath );
wp_update_attachment_metadata( $attach_id,  $metadata );

if ( is_wp_error( $metadata ) )
echo 'Images not upoaded Correctly Please try Again.';
if ( empty( $metadata ) )
echo 'Unknown failure reason.';

return $attach_id;
}

No comments:

Post a Comment