我会通过钩住save_post
操作,而不是javascript。
主要的想法是检查这两个值是否都存在(您的单选按钮被选中post_thumbnail
,如果没有,则将帖子设置为草稿,如果用户未满足要求,则显示信息。
首先,钩入save_post
操作,并检查单选按钮是否具有您的值,以及是否选择了Post缩略图。如果一切正常,则无需执行任何其他操作,但在特殊情况下,您必须阻止发布帖子,并显示错误消息以通知用户。
<?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>";
}
?>