SHARE
TWEET

Untitled

a guest Apr 21st, 2019 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3.  
  4. // vars
  5. $post_id = get_the_id();
  6. $images = get_field('gallery');
  7. $caption = get_field('caption');
  8. $content = get_the_content();
  9.  
  10. // var_dump($post_id);
  11. // var_dump($images);
  12. // var_dump($caption);
  13.  
  14. if(empty($images)):
  15.  
  16.  
  17.     function get_attachment_id( $url ) {
  18.  
  19.     $attachment_id = 0;
  20.  
  21.     $dir = wp_upload_dir();
  22.  
  23.     if ( false !== strpos( $url, $dir['baseurl'] . '/' ) ) { // Is URL in uploads directory?
  24.  
  25.         $file = basename( $url );
  26.  
  27.         $query_args = array(
  28.             'post_type'   => 'attachment',
  29.             'post_status' => 'inherit',
  30.             'fields'      => 'ids',
  31.             'meta_query'  => array(
  32.                 array(
  33.                     'value'   => $file,
  34.                     'compare' => 'LIKE',
  35.                     'key'     => '_wp_attachment_metadata',
  36.                 ),
  37.             )
  38.         );
  39.  
  40.         $query = new WP_Query( $query_args );
  41.  
  42.         if ( $query->have_posts() ) {
  43.  
  44.             foreach ( $query->posts as $post_id ) {
  45.  
  46.                 $meta = wp_get_attachment_metadata( $post_id );
  47.  
  48.                 $original_file       = basename( $meta['file'] );
  49.                 $cropped_image_files = wp_list_pluck( $meta['sizes'], 'file' );
  50.  
  51.                 if ( $original_file === $file || in_array( $file, $cropped_image_files ) ) {
  52.                     $attachment_id = $post_id;
  53.                     break;
  54.                 }
  55.  
  56.             }
  57.  
  58.         }
  59.  
  60.     }
  61.  
  62.     return $attachment_id;
  63.     }
  64.  
  65.     function linkExtractor($html){
  66.     $linkArray = array();
  67.     if(preg_match_all('/<img\s+.*?src=[\"\']?([^\"\' >]*)[\"\']?[^>]*>/i',$html,$matches,PREG_SET_ORDER)){
  68.     foreach($matches as $match){
  69.         if(!empty($match)){
  70.     array_push($linkArray,array($match[1],$match[2]));
  71.         }
  72.     }
  73.     }
  74.     return $linkArray;
  75.     }
  76.     $icount = 0;
  77.     $image_arrray = linkExtractor($content);
  78.     $attachments_array = array();
  79.     foreach($image_arrray as $image){
  80.     $icount++;
  81.     $url = $image[0];
  82.     // echo "<br><strong>Image ".$icount.":</strong> ";
  83.     // echo $url;
  84.     // echo "<br>";
  85.     // echo "<br><strong>Image ".$icount." ID:</strong> ";
  86.     $imageid =  get_attachment_id($url);
  87.     // echo $imageid;
  88.     $attachments_array[] = $imageid;
  89.     }
  90.     update_field('gallery',$attachments_array,$post_id)
  91.  
  92. endif; // images
  93.  
  94. if(empty($caption)):
  95.  
  96.     // text to caption field
  97.     $withptags = wpautop($content);
  98.     $caption = strip_tags($withptags);
  99.     // echo($caption);
  100.     update_field('caption',$caption,$post_id);
  101.  
  102. endif; // caption
  103. ?>
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
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top