5

I have customised my Wordpress site design to use the featured image for posts quite excessively. This is why I need to require all post made by non-admins to require a set featured image.

How is this possible?

Gary Woods
  • 1,011
  • 1
  • 15
  • 33

3 Answers3

6

You need to hook the Publish Action in a custom PlugIn you write. Although this is to require a title, this should get you started, you just need to check if a featured image was assigned.

add_action( 'pre_post_update', 'bawdp_dont_publish' );

function bawdp_dont_publish()
{
    global $post;
    if ( strlen( $post->title ) < 10 ) {
        wp_die( 'The title of your post have to be 10 or more !' );
    }
}

Look at (has_post_thumbnail( $post->ID )) to determine if a post has a featured image.

brasofilo
  • 25,496
  • 15
  • 91
  • 179
Gary
  • 2,866
  • 1
  • 17
  • 20
  • I love your name. I ended up using a plugin but thank you for the respond. I have accepted and upvoted it :) – Gary Woods Sep 17 '12 at 15:50
5

Given Gary's example above, I've written the following to my functions.php file:

function featured_image_requirement() {
     if(!has_post_thumbnail()) {
          wp_die( 'You forgot to set the featured image. Click the back button on your browser and set it.' ); 
     } 
}
add_action( 'pre_post_update', 'featured_image_requirement' );

I'd much rather see this in a plugin as well - there's one called Mandatory Field but it doesn't work with scheduled posts. Both are not really eloquent solutions.

Douglas Karr
  • 123
  • 1
  • 7
2

you can use a plugin

https://wordpress.org/plugins/require-featured-image/

or you can copy and paste below code in your wordpress theme functions.php file:

<?php
/**
 * Require a featured image to be set before a post can be published.
 */
add_filter( 'wp_insert_post_data', function ( $data, $postarr ) {
    $post_id              = $postarr['ID'];
    $post_status          = $data['post_status'];
    $original_post_status = $postarr['original_post_status'];
    if ( $post_id && 'publish' === $post_status && 'publish' !== $original_post_status ) {
        $post_type = get_post_type( $post_id );
        if ( post_type_supports( $post_type, 'thumbnail' ) && ! has_post_thumbnail( $post_id ) ) {
            $data['post_status'] = 'draft';
        }
    }
    return $data;
}, 10, 2 );
add_action( 'admin_notices', function () {
    $post = get_post();
    if ( 'publish' !== get_post_status( $post->ID ) && ! has_post_thumbnail( $post->ID ) ) { ?>
        <div id="message" class="error">
            <p>
                <strong><?php _e( 'Please set a Featured Image. This post cannot be published without one.' ); ?></strong>
            </p>
        </div>
    <?php
    }
} );
Zub
  • 23
  • 3
  • This is very good, it saves the post only as draft until a featured image is chosen. – herrfischer Feb 14 '17 at 14:15
  • Can anyone explain to me why the reverse isn't true - why removing the ! (from before has_post_thumbnail( $post_id )) doesn't save the post as draft if an image IS set? – JohnG Dec 18 '19 at 12:12
  • Couple of problems with this in current wordpress. While it does block changing the status from draft to published, it doesn't cancel the "page published" notice from also appearing and the notice isn't really scoped. – Dtipson Apr 13 '20 at 01:29