Gary Woods Gary Woods - 4 months ago 12
PHP Question

Require authors to set featured image for post

I have customised my Wordpress site design to use the featured image for posts quite excessively. This is why I need to require all post made by non-admins to require a set featured image.

How is this possible?

Answer

You need to hook the Publish Action in a custom PlugIn you write. Although this is to require a title, this should get you started, you just need to check if a featured image was assigned.

add_action( 'pre_post_update', 'bawdp_dont_publish' );

function bawdp_dont_publish()
{
    global $post;
    if ( strlen( $post->title ) < 10 ) {
        wp_die( 'The title of your post have to be 10 or more !' );
    }
}

Look at (has_post_thumbnail( $post->ID )) to determine if a post has a featured image.

Comments