Make featured image required

Is it possible to force user to set featured image on some types of posts. For example I’ve got custom post type mm_photo and I want to show some error message or somehow block user from publishing or updating post when there is no featured image set.

Related posts

Leave a Reply

4 comments

  1. Fairly simple using jQuery and global $typenow ex:

    add_action('admin_print_scripts-post.php', 'my_publish_admin_hook');
    add_action('admin_print_scripts-post-new.php', 'my_publish_admin_hook');
    function my_publish_admin_hook(){
        global $typenow;
        if (in_array($typenow, array('post','page','mm_photo '))){
            ?>
            <script language="javascript" type="text/javascript">
                jQuery(document).ready(function() {
                    jQuery('#post').submit(function() {
                        if (jQuery("#set-post-thumbnail").find('img').size() > 0) {
                            jQuery('#ajax-loading').hide();
                            jQuery('#publish').removeClass('button-primary-disabled');
                            return true;
                        }else{
                            alert("please set a featured image!!!");
                            jQuery('#ajax-loading').hide();
                            jQuery('#publish').removeClass('button-primary-disabled');
                            return false;
                        }
                        return false;
                    });
                });
            </script>
    
            <?php
        }
    }
    
  2. Try the answer here: https://stackoverflow.com/a/13575967

    To repeat the code from that answer:

    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' );
    
  3. This will not publish the post without the featured image.

    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 );
    

    This will notify admin Feature image is required.

    global $pagenow;
    if ( $pagenow == 'post-new.php' || $pagenow == 'post.php' ) :
    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 }
    } );
    endif;
    
  4. f you would like to require that all posts have a featured image before they can be published add this snippet to the functions.php of your wordpress theme. When you try and publish a post without a featured image you get an admin message “You must select Featured Image. Your Post is saved but it can not be published.”

    add_action('save_post', 'heap_child_check_thumbnail');
    add_action('admin_notices', 'heap_child_thumbnail_error');
    
    
    function heap_child_check_thumbnail($post_id) {
    
      // change to any custom post type
      if (get_post_type($post_id) != 'mm_photo') {
        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', 'heap_child_check_thumbnail');
        // update the post set it to draft
        wp_update_post(array('ID' => $post_id, 'post_status' => 'draft'));
        add_action('save_post', 'heap_child_check_thumbnail');
      }
      else {
        delete_transient("has_post_thumbnail");
      }
    
    }
    
    function heap_child_thumbnail_error() {
      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");
      }
    }