lolitaloco

set post thumb if missing on post save (wordpress)

Apr 25th, 2012
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.53 KB | None | 0 0
  1. function init_set_thumb() {
  2.     add_action('save_post', 'set_post_thumb', 10);
  3. }  
  4. add_action('admin_init', 'init_set_thumb');
  5.  
  6. function set_post_thumb($post_id) {
  7.     if(!wp_is_post_revision($post_id)) {
  8.         $post_type = get_post_type($post_id);
  9.         $post_thumb = get_post_thumbnail_id($post_id);
  10.         if($post_type == 'post' && !$post_thumb) { // if post type = post and get_post_thumbnail == false
  11.             $defaultimage = 1; // set to id of attachment to use as default post thumb         
  12.             set_post_thumbnail($post_id,$defaultimage);
  13.         }          
  14.     }
  15. }
Add Comment
Please, Sign In to add comment