Upload featured image on custom post from wordpress front-end

Upload featured image on custom post from wordpress front-end

When you are working with wordpress custom post type and you need to allow user can submit new posts where On each post you need to upload photo of the post also that can be a featured image then you need to follow below codes to upload attachmends and to make it featured.

//Just uploading photo or attachments
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
         

//$file_handler = 'upload_attachment' //Form attachment Field name.
$attach_id = media_handle_upload( $file_handler, $post_id );


//making it featured!
set_post_thumbnail($post_ID, $attach_id );
or
update_post_meta($post_id,'_thumbnail_id',$attach_id);

Leave a Reply

  • (will not be published)