Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

As the title says , I want a plugin/function to prevent/inform the user when he tries to publish the post without setting the featured image.

ANY HELP ???

share|improve this question
add comment

2 Answers 2

<?php
// Something like that should help, but you'll have to play with it to get it working:
// inside your functions.php file
function wpse16372_prevent_publish()
{
    if ( ! is_admin() )
        return;

    // This should be ok, but should be tested:
    $post_id = $GLOBALS['post']->ID;
    echo '<pre>Test for post ID: '; print_r( $post_id ); echo '</pre>';// the actual test

    // has_post_thumbnail() doesn't work/exist on/for admin screens (see your error msg). You need to find another way to test if the post has a thumbnail. Maybe some Javascript?
    //if ( ! has_post_thumbnail( $post_id );
    if ( ! has_post_thumbnail( $post_id ) )
    {
        ?>
        <!-- // 
        <script language="javascript" type="text/javascript">
            alert( 'you have to use a featured image' );
        </script>
        // -->
        <?php
        exit; // abort
    }
}
add_action( 'save_post', 'wpse16372_prevent_publish', 100 );
?>
share|improve this answer
    
But it could be that the has_post_thumbnail won't work if it hasn't already got one. I asume that save_post doesn't get fired on publish_post hook. –  kaiser May 4 '11 at 23:37
    
That is a pretty neat idea. Could it be modified to simply "Alert" vs. aborting or not-publishing the post? I'd like something to come up if the client forgets, but don't necessarily want to stop anything from being saved. –  RodeoRamsey May 5 '11 at 4:29
    
Oh, it already alerts and then aborts (in case js is not activated). –  kaiser May 5 '11 at 13:37
    
Thanks a lot Kaiser for your help, but whenever I try to paste the code and log in to admin panel it gives this error Parse error: syntax error for the line if ( ! has_post_thumbnail( $GLOBALS['post']->ID ); Can you help me ? –  BIALY May 5 '11 at 22:35
1  
Ok, thank you very much for your time :) –  BIALY May 6 '11 at 0:25
show 1 more comment

The has_post_thumbnail() works for me, in WP versions 3.4.1 and other most recently. But in this logic, because the WP will publish the post even with exit or wp_die() or anything to terminate the PHP script. For prevent that the post stay with published status, you will need to update the post before terminate. Look the code below:

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($evento->post_status == 'publish' && !has_post_thumbnail($evento_id)) {
        $post->post_status = 'draft';
        wp_update_post($post);

        $message = '<p>Please, add a thumbnail!</p>'
                 . '<p><a href="' . admin_url('post.php?post=' . $evento_id . '&action=edit') . '">Go back and edit the post</a></p>';
        wp_die($message, 'Error - Missing thumbnail!');
    }               
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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