Ticket #12238: post.php.diff

File post.php.diff, 2.1 KB (added by Amit_k, 8 months ago)

Creates function get_attachment() to retrive image info

  • wp-includes/post.php

    diff --git a/wp-includes/post.php b/wp-includes/post.php
    index 7b5fb7e..2a5e7e2 100644
    a b function _prime_post_caches( $ids, $update_term_cache = true, $update_meta_cache 
    54085408                update_post_caches( $fresh_posts, 'any', $update_term_cache, $update_meta_cache ); 
    54095409        } 
    54105410} 
     5411 
     5412/** 
     5413 * Retrieves attachment data. 
     5414 * 
     5415 * You can optionally pass the image id to retrive data outside the loop 
     5416 * 
     5417 * The function uses get_post() & get_post_meta() 
     5418 * 
     5419 * If size( eg.- thumbnail) is specified it replaces the src, width, height params with new 
     5420 * 
     5421 * @param int $image_id Image ID. 
     5422 * @param string $size The image size. 
     5423 * @return array|bool the data (array) on success or false (bool) 
     5424 */ 
     5425function get_attachement($image_id = NULL, $size = NULL) { 
     5426 
     5427        $id = isset($image_id) ? $image_id : get_the_id(); 
     5428 
     5429        if ( !$attach_data = get_post($id) ) 
     5430                return false; 
     5431        if ( 'attachment' != $attach_data->post_type ) 
     5432                return false; 
     5433        if( !$attach_meta = get_post_meta($id) ) 
     5434                return $attach_data; 
     5435 
     5436        $attach_data = array_merge( (array)$attach_data, $attach_meta ); 
     5437        $meta_data = maybe_unserialize( $attach_data['_wp_attachment_metadata'][0] ); 
     5438 
     5439        $attachment = array( 
     5440                                                'ID'                    =>      $attach_data['ID'] 
     5441                                        ,       'date'                  =>      $attach_data['post_date'] 
     5442                                        ,       'title'                 =>  $attach_data['post_title'] 
     5443                                        ,       'description'   =>      $attach_data['post_content'] 
     5444                                        ,       'caption'               =>      $attach_data['post_excerpt'] 
     5445                                        ,       'src'                   =>      $attach_data['guid'] 
     5446                                        ,       'alt'                   =>      isset( $attach_data['_wp_attachment_image_alt'][0] ) ? $attach_data['_wp_attachment_image_alt'][0] : '' 
     5447                                        ,       'width'                 =>      $meta_data['width'] 
     5448                                        ,       'height'                =>      $meta_data['height'] 
     5449                                        ,       'meta'                  =>      $meta_data['image_meta'] 
     5450                                ); 
     5451 
     5452        if( !isset($size) ) 
     5453                return $attachment;      
     5454 
     5455        $image_size = wp_get_attachment_image_src($id, $size); 
     5456 
     5457        if( !is_array( $image_size ) ) 
     5458                return $attachment; 
     5459 
     5460        $attachment = array_merge($attachment, array( 
     5461                                                'src'                   =>      $image_size[0] 
     5462                                        ,       'width'                 =>      $image_size[1] 
     5463                                        ,       'height'                =>      $image_size[2] 
     5464                                )); 
     5465 
     5466        if( isset($attachment) ) 
     5467                return $attachment; 
     5468 
     5469        return false; 
     5470         
     5471}