Require featured image

I would do this by hooking into the save_post action, not by javascript.

The main idea is to check if both values are there (Your Radiobutton is selected AND the post_thumbnail is present), and set the post to draft if they are not, as well as displaying an Information if the user does not fulfill the requirements.

First, hook into the save_post action, and check if the Radiobutton has your Value, as well as the Post Thumbnail is selected. If everything is okay, you do not need to do anything else, but in your special case you have to prevent the post from being published, as well as display an Error message to inform the user.

<?php
// this function checks if the checkbox and the thumbnail are present
add_action('save_post', 'f711_option_thumbnail');

function f711_option_thumbnail($post_id) {
    // get the value that is selected for your select, don't know the specifics here
    // you may need to check this value from the submitted $_POST data.
    $checkoptionvalue = get_post_meta( $post_id, "yourmetaname", true );
    if ( !has_post_thumbnail( $post_id ) && $checkoptionvalue == "yourvalue" ) {
        // set a transient to show the users an admin message
        set_transient( "post_not_ok", "not_ok" );
        // unhook this function so it doesn't loop infinitely
        remove_action('save_post', 'f711_option_thumbnail');
        // update the post set it to draft
        wp_update_post(array('ID' => $post_id, 'post_status' => 'draft'));
        // re-hook this function
        add_action('save_post', 'f711_option_thumbnail');
    } else {
        delete_transient( "post_not_ok" );
    }
}

// add a admin message to inform the user the post has been saved as draft.

function showAdminMessages()
{
            // check if the transient is set, and display the error message
    if ( get_transient( "post_not_ok" ) == "not_ok" ) {
        // Shows as an error message. You could add a link to the right page if you wanted.
        showMessage("You need to select a Post Thumbnail if you choose this Option. Your Post was not published.", true);
                // delete the transient to make sure it gets shown only once.
        delete_transient( "post_not_ok" );
    }

}   
add_action('admin_notices', 'showAdminMessages');       
// function to display the errormessage
function showMessage($message, $errormsg = false)
{
    if ($errormsg) {
        echo '<div id="message" class="error">';
    }
    else {
        echo '<div id="message" class="updated fade">';
    }

    echo "<p><strong>$message</strong></p></div>";

}   


?>

Leave a Comment