Created
December 15, 2019 12:46
-
-
Save mihdan/cdc1537f1db4d7d3aa49ad363f3c8a1a to your computer and use it in GitHub Desktop.
Автоматическая генерация обложки записи из первой прикрепленной к посту фотке.
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 | |
/** | |
* Plugin Name: Auto Post Thumbnail | |
* Author: [email protected] | |
*/ | |
add_action( | |
'save_post', | |
function ( $post_id, WP_Post $post ) { | |
if ( wp_is_post_revision( $post_id ) || wp_is_post_autosave( $post_id ) ) { | |
return; | |
} | |
if ( has_post_thumbnail( $post_id ) ) { | |
return; | |
} | |
$attachments = get_attached_media( 'image', $post_id ); | |
if ( $attachments ) { | |
$keys = array_reverse( $attachments ); | |
set_post_thumbnail( $post_id, $keys[0]->ID ); | |
}; | |
}, | |
10, | |
2 | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment