admin管理员组

文章数量:1322838

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.

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.

Share Improve this question asked Nov 30, 2012 at 13:52 AdamAdam 3612 gold badges4 silver badges4 bronze badges 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 Commented Nov 30, 2012 at 14:23
  • 1 Try to adapt this and update your Question. And this will help you write a better Q. – brasofilo Commented Nov 30, 2012 at 14:32
Add a comment  | 

4 Answers 4

Reset to default 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
    }
}

Try the answer here: https://stackoverflow/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' );

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;

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

本文标签: custom post typesMake featured image required