Created
February 28, 2017 16:10
-
-
Save rmpel/a1164225e369f0745068c0ba62b20841 to your computer and use it in GitHub Desktop.
Update more meta when Enable Media Replace replaces a file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // this gist is an example for updating a custom array-meta-data whenever EMR replaces a media file and renames it. | |
| add_action('enable-media-replace-upload-done', 'more_to_do_emr'); | |
| function more_to_do_emr( $new_guid ) { | |
| global $wpdb; | |
| // get attachments meta data | |
| $attachment_id = (int) $_POST["ID"]; | |
| if (!$attachment_id) | |
| return; | |
| $meta_ids = $wpdb->get_col("SELECT meta_id FROM {$wpdb->postmeta} where meta_key = 'attachments' AND meta_value LIKE '%i:{$attachment_id};%'"); | |
| foreach ($meta_ids as $meta_id) { | |
| $meta = get_post_meta_by_id( $meta_id ); | |
| if (is_array($meta->meta_value) && count($meta->meta_value) > 0) { | |
| foreach ($meta->meta_value as $id => $value) { | |
| if ($id == $attachment_id) { | |
| $meta->meta_value[ $id ] = wp_get_attachment_url( $attachment_id ); | |
| } | |
| } | |
| update_post_meta( $meta->post_id, $meta->meta_key, $meta->meta_value ); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment