Advertisement
mattiasmadis

Untitled

Mar 28th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.80 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. Plugin Name: WP All Import - Additional Variation images
  5. Description: Importing additional variation images with WP ALL IMPORT plugin
  6. Version: 1.0
  7. Author: Disain24 OƜ - Kristo Meerits
  8. */
  9.  
  10. // Include'ing WP ALL IMPORT structure
  11. include "rapid-addon.php";
  12. $d24_images_addon = new RapidAddon('Additional variation images', 'd24_images_addon');
  13.  
  14. // Multiple images upload form
  15. //$d24_images_addon->import_images( 'd24_gallery', 'All additional variation images', 'image' );
  16.  
  17.  
  18. // First image upload form
  19. $d24_images_addon->add_field('d24_variation_image1', 'Additional variation image # 1', 'image');
  20.  
  21. // Second image upload form
  22. $d24_images_addon->add_field('d24_variation_image2', 'Additional variation image # 2', 'image');
  23.  
  24. // Third image upload form
  25. $d24_images_addon->add_field('d24_variation_image3', 'Additional variation image # 3', 'image');
  26.  
  27. // Fourth image upload form
  28. $d24_images_addon->add_field('d24_variation_image4', 'Additional variation image # 4', 'image');
  29.  
  30. // Fifth image upload form
  31. $d24_images_addon->add_field('d24_variation_image5', 'Additional variation image # 5', 'image');
  32.  
  33. // Sixth image upload form
  34. $d24_images_addon->add_field('d24_variation_image6', 'Additional variation image # 6', 'image');
  35.  
  36. // For importing
  37. $d24_images_addon->set_import_function('d24_variation_images_import');
  38.  
  39. // Lets run all actions befrore and show form in WP All import plugin. Nothing will be imported yet.
  40. $d24_images_addon->run();
  41.  
  42. // Create import functions for importing
  43. function d24_variation_images_import($post_id, $data, $import_options) {
  44.  
  45. global $d24_images_addon;
  46.  
  47. // Prepare functions and progress then update post_meta
  48. // Also get uploaded image ID with progressing image filename
  49. // Source from Github: https://gist.github.com/wpscholar/3b00af01863c9dc562e5#file-get-attachment-id-php
  50.  
  51. // Create array
  52. $gallery_array = array("post_ID" => $post_id, "gallery" => array() );
  53.  
  54. // First image progressing and uploading
  55. if ($d24_images_addon->can_update_image($import_options)) {
  56. $image_url = wp_get_attachment_url($data['d24_variation_image1']['attachment_id']);
  57. $attachment_id = get_attachment_id( $image_url );
  58. array_push($gallery_array["gallery"], $attachment_id);
  59.  
  60. if( get_post_meta( $post_id, 'd24_variation_image1' ) ){
  61. update_post_meta( $post_id, 'd24_variation_image1', $attachment_id );
  62. } else {
  63. add_post_meta( $post_id, 'd24_variation_image1', $attachment_id );
  64. }
  65. }
  66.  
  67. // Second image progressing and uploading
  68. if ($d24_images_addon->can_update_image($import_options)) {
  69. $image_url = wp_get_attachment_url($data['d24_variation_image2']['attachment_id']);
  70. $attachment_id = get_attachment_id( $image_url );
  71. array_push($gallery_array["gallery"], $attachment_id);
  72.  
  73. if( get_post_meta( $post_id, 'd24_variation_image2' ) ){
  74. update_post_meta( $post_id, 'd24_variation_image2', $attachment_id );
  75. } else {
  76. add_post_meta( $post_id, 'd24_variation_image2', $attachment_id );
  77. }
  78. }
  79.  
  80. // Third image progressing and uploading
  81. if ($d24_images_addon->can_update_image($import_options)) {
  82. $image_url = wp_get_attachment_url($data['d24_variation_image3']['attachment_id']);
  83. $attachment_id = get_attachment_id( $image_url );
  84. array_push($gallery_array["gallery"], $attachment_id);
  85.  
  86. if( get_post_meta( $post_id, 'd24_variation_image3' ) ){
  87. update_post_meta( $post_id, 'd24_variation_image3', $attachment_id );
  88. } else {
  89. add_post_meta( $post_id, 'd24_variation_image3', $attachment_id );
  90. }
  91. }
  92.  
  93. // Fourth image progressing and uploading
  94. if ($d24_images_addon->can_update_image($import_options)) {
  95. $image_url = wp_get_attachment_url($data['d24_variation_image4']['attachment_id']);
  96. $attachment_id = get_attachment_id( $image_url );
  97. array_push($gallery_array["gallery"], $attachment_id);
  98.  
  99. if( get_post_meta( $post_id, 'd24_variation_image4' ) ){
  100. update_post_meta( $post_id, 'd24_variation_image4', $attachment_id );
  101. } else {
  102. add_post_meta( $post_id, 'd24_variation_image4', $attachment_id );
  103. }
  104. }
  105.  
  106. // Fifth image progressing and uploading
  107. if ($d24_images_addon->can_update_image($import_options)) {
  108. $image_url = wp_get_attachment_url($data['d24_variation_image5']['attachment_id']);
  109. $attachment_id = get_attachment_id( $image_url );
  110. array_push($gallery_array["gallery"], $attachment_id);
  111.  
  112. if( get_post_meta( $post_id, 'd24_variation_image5' ) ){
  113. update_post_meta( $post_id, 'd24_variation_image5', $attachment_id );
  114. } else {
  115. add_post_meta( $post_id, 'd24_variation_image5', $attachment_id );
  116. }
  117. }
  118.  
  119. // Sixth image progressing and uploading
  120. if ($d24_images_addon->can_update_image($import_options)) {
  121. $image_url = wp_get_attachment_url($data['d24_variation_image6']['attachment_id']);
  122. $attachment_id = get_attachment_id( $image_url );
  123.  
  124. array_push($gallery_array["gallery"], $attachment_id);
  125.  
  126. if( get_post_meta( $post_id, 'd24_variation_image6' ) ){
  127. update_post_meta( $post_id, 'd24_variation_image6', $attachment_id );
  128. } else {
  129. add_post_meta( $post_id, 'd24_variation_image6', $attachment_id );
  130. }
  131. }
  132.  
  133. // Update gallery metakey
  134. $gallery_array_to_string = implode(',', $gallery_array["gallery"]);
  135.  
  136. if( get_post_meta( $post_id, 'variation_image_gallery' ) ){
  137. update_post_meta( $post_id, 'variation_image_gallery', $gallery_array_to_string );
  138. } else {
  139. add_post_meta( $post_id, 'variation_image_gallery', $gallery_array_to_string );
  140. }
  141.  
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement