7

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.

2
  • Have you also considered using fall-back images in your template if none is supplied? There was a similar question/answer posted here – Zach Nov 30 '12 at 14:23
  • 1
    Try to adapt this and update your Question. And this will help you write a better Q. – brasofilo Nov 30 '12 at 14:32
4

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

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

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.