Last active
August 29, 2015 14:24
-
-
Save davidvandenbor/3355b1f9b461753c2152 to your computer and use it in GitHub Desktop.
Auto generate featured WP image
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* generate featured image automatically | |
* @author @david <[email protected]> | |
*/ | |
function autogen_featured_img() { | |
global $post; | |
if (!has_post_thumbnail($post->ID)) { | |
$attached_image = | |
get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" ); | |
if ($attached_image) { | |
foreach ($attached_image as $attachment_id => $attachment) { | |
set_post_thumbnail($post->ID, $attachment_id); | |
} | |
} | |
} | |
} | |
/*This line is used to generate featured images for all old | |
posts. Remove this once the default images get generated | |
for all of the old posts*/ | |
add_action('the_post', 'autogen_featured_img'); | |
/* For new upcoming posts, leave them permanently*/ | |
add_action('save_post', 'autogen_featured_img'); | |
add_action('draft_to_publish', 'autogen_featured_img'); | |
add_action('new_to_publish', 'autogen_featured_img'); | |
add_action('pending_to_publish', 'autogen_featured_img'); | |
add_action('future_to_publish', 'autogen_featured_img'); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment