Set Featured Image Front Frontend Form

you can do that by running the function

set_post_thumbnail( $my_post_id, $thumbnail_id );

remember, you have to process and insert the image into the library first:

$uploaddir = wp_upload_dir();
$file = $_FILES[ ... whatever you have in your POST data ... ];
$uploadfile = $uploaddir['path'] . "https://wordpress.stackexchange.com/" . basename( $file );

move_uploaded_file( $file , $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 );

did not test the script – it is adapted from a version i created. maybe you want to change the filename and stuff like that, but all in all it works this way 🙂

Leave a Comment