Which action hook can I use when a featured image has been selected

I have made a plugin for wordpress which copies the image to another directory when a post has been edit. But if you only upload another featured image and don’t save the post. The image has been attached to the post, but the post has not been updated. Which does not trigger my plugin function. Show which trigger (action hook) can I use to also copie the featured image?

The action which I now use for the post edit is wp_insert_post().

I forgot the reason why I use wp_insert_post(), but maybe that’s why it fails 😉

1 Answer
1

The set_post_thumbnail function uses the metadata functions to set the featured image.

You have two actions to hook into that process:

EDIT: The action hooks are now defined different

Thanks @dalbaeb!

  • update_postmeta, before the data is written into the database. Previously update_post_meta
  • updated_postmeta, after the data is written into the database. Previously updated_post_meta

SECOND EDIT: No need to panic

updated_{$meta_type}_meta and update_{$meta_type}_meta still work.

You will have to make a conditional, and be good to go:

if ( $metakey == '_thumbnail_id' ) { /*blabla*/ }

Leave a Comment