Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

Change post thumbnail using front end post edit form

it is much easier with WordPress inbuilt function media_handle_upload

http://codex.wordpress.org/Function_Reference/media_handle_upload

// These files need to be included as dependencies when on the front end.
require_once( ABSPATH . 'wp-admin/includes/image.php' );
require_once( ABSPATH . 'wp-admin/includes/file.php' );
require_once( ABSPATH . 'wp-admin/includes/media.php' );

// Let WordPress handle the upload.
// Remember, 'my_image_upload' is the name of our file input in our form above.
$attachment_id = media_handle_upload( 'my_image_upload', $_POST['post_id'] );

if ( is_wp_error( $attachment_id ) ) {
    // There was an error uploading the image.
} else {
    // The image was uploaded successfully!
}

You will need to specify your file control name, then you can call set_post_thumbnail function OR set post meta ‘_thumbnail_id’

set_post_thumbnail( $post_id, $attachment_id );

EDIT:

For some reason, on the post edit screen the image file does not even upload.

Can you double check that you have properly set enctype=”multipart/form-data” attribute to your form tag?

Related Posts:

  1. Programmatically set thumbnail via URL using DOM and XPATH – Where to add ELSEIF
  2. Unable to set Custom “Link URL” for Featured Image, and More?
  3. wp_get_attachment_metadata()[‘image_meta’] fetch WP metadata or EXIF?
  4. Copy wp_postmeta from one database to another
  5. How do I set a featured image (thumbnail) by image URL when using wp_insert_post()?
  6. How do I get image url only on the_post_thumbnail
  7. How to get image title/alt attribute?
  8. How to Fix HTTP Error When Uploading Images?
  9. Add class name to post thumbnail
  10. How to disable WordPress from creating thumbnails?
  11. How to retrieve image from URL and set as featured image/post thumbnail
  12. How to set featured image to custom post from outside programmatically
  13. How do you remove hard coded thumbnail image dimensions?
  14. How do you get the post thumbnail size?
  15. Setting a post’s featured image from an embedded YouTube video
  16. How to get featured image’s width and use elsewhere in template?
  17. set_post_thumbnail_size vs add_image_size
  18. Minimum Dimensions Requirement for Featured Image?
  19. Generate Thumbnails only for Featured Images
  20. How to get all featured image sizes and their URLs?
  21. How to Dynamically Resize WordPress Image On-The-Fly (custom field/theme option)
  22. How to add post featured image to RSS item tag?
  23. How do I regenerate thumbnails?
  24. Display thumbnail only if requested size exists
  25. Delete original image – keep thumbnail?
  26. the_post_thumbnail responsive srcset not populating with custom image size
  27. Set custom name for generated thumbnails
  28. Post thumbnail alt title
  29. Get Featured Image via direct sql query
  30. Get featured image on Blog Index
  31. the_post_thumbnail hard cropping not working no matter what
  32. Can I refresh the thumbnails programmatically?
  33. How to add multiple image sizes of the same size with add_image_size
  34. New image size version only for the Featured Image file
  35. wp_get_attachment_image_src always returns full-sized image
  36. Get featured image URL by page id
  37. change set_post_thumbnail_size according to post type admin page
  38. Add Title Attribute to WordPress Image the_post_thumbnail
  39. Can’t extract and set SVG dimensions
  40. Set Featured Image Front Frontend Form
  41. Getting Thumbnail Path rather than Image Tag
  42. Display Category Thumbnail and links in Woo commerce
  43. the_post_thumbnail with lazyload JQ plugin
  44. Replacing Gutenberg Featured Image control?
  45. Display Post Thumbnail Without Being Featured Image
  46. Featured Image of Video from oembed
  47. oEmbed, thumbnails and wordpress
  48. wp_get_attachment_image_src() and custom sizes
  49. Prevent WordPress from cropping GIF files
  50. media_sideload_image generates blank?
  51. Post thumbnail outside the loop
  52. What’s the difference between post thumbnails and featured images (if any)?
  53. Getting custom-sized featured image’s URL?
  54. What is your take: TimThumb vs. WordPress Thumbnails? The great debate [closed]
  55. How to crop thumbnail height to auto with set width
  56. Retrieve 1st image in post and set it as featured image, when post is saved/updated
  57. Use thumbnail image using url of that image
  58. Retrieve featured image as object
  59. How do I add the featured image to the_content after the first paragraph?
  60. Showing a placeholder/default img, if no featured image is set
  61. Re-order media links?
  62. Multiple featured image thumbnails for post types (Multiple Post Thumbnails plugin)
  63. the_post_thumbnail scaling not hard cropping
  64. Skip posts without a thumbnail in loop
  65. wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); Getting thumb from wrong loop
  66. Set featured image for multiple posts
  67. Image uploader with “Set Featured Image” link on front end
  68. adding an id to the_post_thumbnail
  69. Featured image size in ADMIN panel?
  70. Featured image with the same height thumbs returns wrong thumb
  71. How can I display my featured image correctly inside my single posts?
  72. How to change the naming of the resized image files
  73. Featured image upload finished hook
  74. How to get featured post title & image using JSON API?
  75. Generate thumbnail for images with the same size as thumbnails
  76. Set post thumbnail as background
  77. Display thumbnail only on the very first post in the loop?
  78. How to show featured image CAPTION only if it exist
  79. Set post-thumbnail (with php)?
  80. get_thumbnail_id returns full size image
  81. Implementing Zurb’s Interchange Into WordPress
  82. can the_excerpt function also get images?
  83. Is there a hook / action that is triggered when adding or removing a post thumbnail?
  84. How to get_posts having post thumbnail?
  85. Add Image Dimensions to Media Library Tab on Media Upoader
  86. Change Output for Images in Content
  87. Add options to featured image
  88. Featured Image Inherited from Parent Page
  89. Change wording of default thumbnail metabox
  90. Specific featured image for each category
  91. how to pull images with no add_theme_support(‘post_thumbnails’)
  92. Can WordPress crop only the post thumbnails?
  93. Allow user to manually set crop area of default/custom image sizes on upload [closed]
  94. Add post title as alt tag for featured image
  95. get_pages – Display child then grandchild pages
  96. How to replace the_post_thumbnail template tag and show the first inside the post image instead
  97. Display child pages on a parent and child page using Featured Thumbnails
  98. Query the post which have a thumbnail
  99. Featured image for news page
  100. How to get the image sizes, in a function, as per the sizes mentioned in the media settings?
Categories post-thumbnails Tags post-meta, post-thumbnails
Print Post Parent Title
Unable to get paginate_links working with a custom query [duplicate]

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress