Advertisement
meetsos

First Image in Post as Thumbnail (easy-add-thumbnail)

Nov 8th, 2015 (edited)
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.63 KB | None | 0 0
  1. <?php
  2. /*
  3. * Plugin Name: Easy Add Thumbnail
  4. * Plugin URI: http://wordpress.org/extend/plugins/easy-add-thumbnail/
  5. * Description: Checks if you defined the featured image, and if not it sets the featured image to the first uploaded image into that post. So easy like that...
  6. * Author: Samuel Aguilera
  7. * Version: 1.1.1
  8. * Author URI: http://www.samuelaguilera.com
  9. * Requires at least: 3.7
  10. */
  11.  
  12. /*
  13. This program is free software: you can redistribute it and/or modify
  14. it under the terms of the GNU General Public License version 3 as published by
  15. the Free Software Foundation.
  16.  
  17. This program is distributed in the hope that it will be useful,
  18. but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20. GNU General Public License for more details.
  21.  
  22. You should have received a copy of the GNU General Public License
  23. along with this program.  If not, see <http://www.gnu.org/licenses/>.
  24. */
  25.  
  26. if ( function_exists( 'add_theme_support' ) ) {
  27.  
  28. add_theme_support( 'post-thumbnails' ); // This should be in your theme. But we add this here because this way we can have featured images before swicth to a theme that supports them.
  29.      
  30. function easy_add_thumbnail($post) {
  31.          
  32.     $already_has_thumb = has_post_thumbnail();
  33.     $post_type = get_post_type( $post->ID );    
  34.     $exclude_types = array('');
  35.     $exclude_types = apply_filters( 'eat_exclude_types', $exclude_types );
  36.  
  37.     // do nothing if the post has already a featured image set
  38.     if ( $already_has_thumb ) {
  39.         return;
  40.     }
  41.  
  42.     // do the job if the post is not from an excluded type                     
  43.     if ( ! in_array( $post_type, $exclude_types ) )  {
  44.  
  45.         // get first attached image
  46.         $attached_image = get_children( "order=ASC&post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
  47.  
  48.         if ( $attached_image ) {
  49.  
  50.             $attachment_values = array_values( $attached_image );
  51.             // add attachment ID                                            
  52.             add_post_meta( $post->ID, '_thumbnail_id', $attachment_values[0]->ID, true );                                
  53.                            
  54.         }
  55.                            
  56.                          
  57.     }
  58.  
  59. }
  60.  
  61. // set featured image before post is displayed (for old posts)
  62. add_action('the_post', 'easy_add_thumbnail');
  63.  
  64. // hooks added to set the thumbnail when publishing too
  65. add_action('new_to_publish', 'easy_add_thumbnail');
  66. add_action('draft_to_publish', 'easy_add_thumbnail');
  67. add_action('pending_to_publish', 'easy_add_thumbnail');
  68. add_action('future_to_publish', 'easy_add_thumbnail');
  69.  
  70. }
  71.  
  72. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement