Require a featured image before you can publish post

Go to source

snippet :  PHPcopy
add_action('save_post', 'wpds_check_thumbnail');
add_action('admin_notices', 'wpds_thumbnail_error');
function wpds_check_thumbnail($post_id) {
    // change to any custom post type
    if(get_post_type($post_id) != 'post')
        return;
    if ( !has_post_thumbnail( $post_id ) ) {
        // set a transient to show the users an admin message
        set_transient( "has_post_thumbnail", "no" );
        // unhook this function so it doesn't loop infinitely
        remove_action('save_post', 'wpds_check_thumbnail');
        // update the post set it to draft
        wp_update_post(array('ID' => $post_id, 'post_status' => 'draft'));
        add_action('save_post', 'wpds_check_thumbnail');
    } else {
        delete_transient( "has_post_thumbnail" );
    }
}
function wpds_thumbnail_error()
{
    // check if the transient is set, and display the error message
    if ( get_transient( "has_post_thumbnail" ) == "no" ) {
        echo "<div id='message' class='error'><p><strong>You must select Featured Image. Your Post is saved but it can not be published.</strong></p></div>";
        delete_transient( "has_post_thumbnail" );
    }
}

source →

Require a featured image before you can publish post is a post from: wpsnipp.comWordPress Snippets, Code, Hacks, 590+ for your theme, blog
which is not allowed to be copied on other sites.”

Share this post.


<a
href="http://www.facebook.com/sharer.php?u=Require a featured image before you can publish post“>Facebook | <a
href="http://www.stumbleupon.com/submit?url=Require a featured image before you can publish post“>StumbleUpon | <a
href="http://delicious.com/post?url=Require a featured image before you can publish post“>Delicious | <a
href="http://twitter.com/home?status=Reading: Require a featured image before you can publish post @wpsnipp”>Tweet It | <a
href="http://digg.com/submit?url=Require a featured image before you can publish post“>Digg This

© wpsnipp.com All Rights reserved.

source: wpsnipp.com