SHARE
TWEET

Untitled

5ally Nov 3rd, 2018 (edited) 102 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. // Details: https://wordpress.stackexchange.com/questions/316454/copy-attachments-to-another-post-type-and-change-attachment-url/316669#316669
  3. // File: import-pics.php
  4.  
  5. require_once $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php';
  6.  
  7. global $current_user, $imic_options; // Use global
  8. wp_get_current_user(); // Make sure global is set, if not set it.
  9.  
  10. $yourID = $current_user->ID;
  11. $propertyID = $_POST['propertyID'];
  12. $vidID = $_POST['videoID'];
  13.  
  14. $path = WP_CONTENT_DIR . '/uploads/' . $yourID . '/' . $vidID;
  15. $path2 = $yourID . '/' . $vidID; // relative to wp-content/uploads
  16. $prop_att_ids = (array) get_post_meta( $propertyID, 'imic_property_sights', false );
  17.  
  18. // Check copied attachments and don't include already copied ones.
  19. $copied_atts = (array) get_post_meta( $vidID, '_copied_imic_property_sights', true );
  20. $copied_atts = array_unique( array_filter( $copied_atts ) );
  21. $to_copy = array_diff( array_unique( array_filter( $prop_att_ids ) ), $copied_atts );
  22. // These two are for testing:
  23. echo 'Old $copied_atts: ' . implode( ', ', $copied_atts ) . '<br>';
  24. echo '$prop_att_ids: ' . implode( ', ', $prop_att_ids ) . '<br>';
  25.  
  26. foreach ( $to_copy as $att_id ) {
  27.     // Check if we have a valid image/attachment.
  28.     if ( $att_id && $file = get_attached_file( $att_id ) ) {
  29.         $filename = basename( $file );
  30.         $file2 = $path . '/' . wp_unique_filename( $path, $filename );
  31.  
  32.         // Copy the image file to $path.
  33.         if ( @copy( $file, $file2 ) ) {
  34.             // Copy the attachment (post) data.
  35.             $att = get_post( $att_id, ARRAY_A );
  36.             unset( $att['ID'] );
  37.             $att_id2 = wp_insert_attachment( $att, $file2 );
  38.  
  39.             // Then add the meta data `vid_pix`.
  40.             add_post_meta( $vidID, 'vid_pix', $att_id2 );
  41.  
  42.             // Copy the attachment's meta data. (no thumbnails)
  43.             $data = wp_get_attachment_metadata( $att_id );
  44.             $data['file'] = $path2 . '/' . basename( $file2 );
  45.             $data['sizes'] = [];
  46.             wp_update_attachment_metadata( $att_id2, $data );
  47.  
  48.             // Add to copied attachments list.
  49.             $copied_atts[ $att_id2 ] = $att_id;
  50.  
  51.             $url = wp_get_attachment_image_url( $att_id2, 'full' );   // test
  52.             echo 'File copied! ' . make_clickable( $url ) . '<br>';   // test
  53.         } else {
  54.             echo "Could not copy <i>$file</i> to <b>$file2</b>!<br>"; // test
  55.         }
  56.     } else {
  57.         echo 'Invalid attachment ID or missing attached file.<br>';   // test
  58.     }
  59. }
  60.  
  61. // Updates copied attachments.
  62. update_post_meta( $vidID, '_copied_imic_property_sights', $copied_atts );
  63.  
  64. // These two are for testing:
  65. echo 'New $copied_atts: ' . implode( ', ', $copied_atts ) . '<br>';
  66. echo 'vid_pix: ' . implode( ', ', get_post_meta( $vidID, 'vid_pix', false ) ) . '<br>';
  67.  
  68. ?>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top