Posted By

krike on 11/07/12


Tagged

class php image wordpress featured


Versions (?)

Wordpress function to upload image and set as featured image


 / Published in: PHP
 

URL: https://github.com/krike/WPelements

A function to upload an image to the media library and set it as featured image of a post. It requires 2 params. The name of the upload field and of course the post ID.

This function is part of the WPelements library class available on Github. Feedback / suggestions / improvements are always welcome.

  1. /**
  2. * Function to upload an image to the media library and set it as the featured image of a post
  3. * @param string Name of the upload field
  4. * @param int ID of the post
  5. * @return string
  6. * */
  7. public function set_featured_image($file, $post_id){
  8. require_once(ABSPATH . "wp-admin" . '/includes/image.php');
  9. require_once(ABSPATH . "wp-admin" . '/includes/file.php');
  10. require_once(ABSPATH . "wp-admin" . '/includes/media.php');
  11. $attachment_id = media_handle_upload($file, $post_id);
  12. update_post_meta($post_id, '_thumbnail_id', $attachment_id);
  13. $attachment_data = array(
  14. 'ID' => $attachment_id
  15. );
  16. wp_update_post($attachment_data);
  17. return $attachment_id;
  18. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: TallyMenning on February 28, 2018

You can ask here - https://www.essay-have.com/buy-research-paper.php . I would definitely recommend using this service.

Posted By: TallyMenning on February 28, 2018

You can ask here - https://www.essay-have.com/buy-research-paper.php . I would definitely recommend using this service.

Posted By: Mannan on June 18, 2018

Choosing the wine opener is equally important to choosing the best wine for you. When it comes to corkscrew with the natural pull of the lever in one direction, you can twist the corkscrew in and pull the cork out of the bottle. the best wine openers

You need to login to post a comment.