Reputation: 277
I am using the following code to REQUIRE a feature image when creating a WordPress post in the dashboard ...
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 work ... but there are two problems/bugs.
POSTS NOT PAGES
First, I only want this to work on POSTS, and not on PAGES. So, when I go to edit any page that I have, and try to submit ... I get the error message requiring a featured image. So I can't save any of my pages... How do I change the code above so it only works when editing a POST (and not a PAGE)
SAVING DRAFT...
The second issue is a problem with WordPress automatically saving drafts. While I am editing a page, the PUBLISH button will fade out (and become unclickable) because the page is "saving draft...". This is a useful feature with WordPress. However, since I enabled the script above, this saving delay is EXTREMELY long. Sometimes the publish buttons doesn't become clickable at all.
Anyone know how to "save the draft" without needed the required feature?
Upvotes: 0
Views: 505
Reputation: 5322
I'm not sure if that's a typo, but you're adding the action twice, so that should only be once.
Anyhow, you can try this:
function featured_image_requirement() {
global $post;
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return;
if ($post - > post_status == "publish" && !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');
Upvotes: 0