4

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 ;)

migrated from stackoverflow.com Dec 20 '12 at 4:44

This question came from our site for professional and enthusiast programmers.

  • Please show your actual code. – kaiser Dec 20 '12 at 8:04
5

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*/ }
  • 2
    I believe now it is updated_postmeta (link) and update_postmeta (link) – montrealist Apr 29 '15 at 18:33
  • 1
    Actually upon further inspection there's also updated_{$meta_type}_meta (link) and update_{$meta_type}_meta (link) actions, so technically all four will work. Sorry about that! – montrealist Apr 29 '15 at 20:40
  • 1
    updated_postmeta doesn't seem to work any longer. After checking the docs, I found attachment_updated was added in WP 4.4: developer.wordpress.org/reference/hooks/attachment_updated – Ian Oct 13 '16 at 16:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.