Require authors to set featured image for post

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?

Related posts

Leave a Reply

3 comments

  1. 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.

  2. 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.

  3. 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
        }
    } );