这个has_post_thumbnail()
适用于我,在WP版本3.4.1和其他最新版本中。但按照这种逻辑,因为WP将发布帖子,即使exit
或wp_die()
或任何终止PHP脚本的操作。为了防止帖子保持已发布状态,您需要在终止之前更新帖子。查看以下代码:
add_action(\'save_post\', \'prevent_post_publishing\', -1);
function prevent_post_publishing($post_id)
{
$post = get_post($post_id);
// You also add a post type verification here,
// like $post->post_type == \'your_custom_post_type\'
if($post->post_status == \'publish\' && !has_post_thumbnail($post_id)) {
$post->post_status = \'draft\';
wp_update_post($post);
$message = \'<p>Please, add a thumbnail!</p>\'
. \'<p><a href="\' . admin_url(\'post.php?post=\' . $post_id . \'&action=edit\') . \'">Go back and edit the post</a></p>\';
wp_die($message, \'Error - Missing thumbnail!\');
}
}