我必须对一个自定义的帖子类型做同样的事情,如果没有元数据,这将是无用的,并且会导致主题问题。
你要么
A、
使用发布后,请检查帖子的元数据
transition_post_status 钩子,如果元数据不存在,则将状态更改为;草稿“;并抛出错误消息,提示他们返回添加它的链接。这种方式涉及到一个单独的错误页面,可以根据多个帖子要求随意设置多条消息的样式。我的代码:
// Validation - make sure teams and team news have the required fields before publishing
// https://wordpress.stackexchange.com/a/187999/127459
// https://developer.wordpress.org/reference/hooks/transition_post_status/#comment-244
// the only issue with this is that it sometimes failed to register changes unless refreshed, so errors that are subsequently fixed would still throw an error until the page is refreshed.
add_action( \'transition_post_status\', \'team_post_changed\', 10, 3 );
function team_post_changed( $new_status, $old_status, $post ) {
// only check Teams or Team News Posts
if ( $post->post_type === \'team\' || $post->post_type === \'team-news\' ) {
// only if it is being published or saved
if ($new_status === \'publish\') {
$count = 0;
$sports = wp_get_object_terms($post->ID, \'sport\');
$gender = wp_get_object_terms($post->ID, \'gender\');
$meta = get_post_meta($post->ID);
$thisCoach = $meta[\'_coach_name\'][0];
if ( $post->post_type === \'team\') {$thisPostType = \'Team\';
} else {$thisPostType = \'Team News Post\';}
$die_message = \'<em>The \'.$thisPostType.\' was not published because of the following errors:</em><hr>\';
$die_title = \'Error - Missing \';
// Team / Team News must have Sport
if (!$sports[1]) {
$die_message .= \'<br><h3>Error - Please Specify a Sport</h3>\';
$die_message .= \'<p>\'.$thisPostType.\'s must be assigned a Season and Sport before they can be published. If the sport for this team is not avaialable, create a new sport <a href="\'.admin_url(\'edit-tags.php?taxonomy=sport&post_type=team\').\'">here</a>, under the correct season.</p>\';
$die_title .= \'Sport\';
$count += 1;
}
// Teams must also have Gender
if ( $post->post_type === \'team\') {
if (!$gender[0]){
if ($count === 1){ $die_title .= \' and \';}
$die_message .= \'<br><h3>Error - Please Specify a Team Gender</h3>\';
$die_message .= \'<p>\'.$thisPostType.\'s must be assigned a Gender before they can be published.</p>\';
$die_title .= \'Gender\';
$count += 1;
}
}
if ($count > 0) {
// keep post from publishing
$post->post_status = \'draft\';
wp_update_post($post);
// provide error message
$die_message .= \'<br><hr><p><a href="\' . admin_url(\'post.php?post=\' . $post->ID . \'&action=edit\') . \'">Go back and edit the post</a></p>\';
wp_die($die_message, $die_title);
}
}
}
}
或
B、
在发布之前通过Ajax检查帖子内容(按下发布按钮时立即检查),并在同一屏幕上提醒用户,除非帖子完全有效。此方法需要Javascript和Jquery。它会向用户发出浏览器模式类型警报,在您按下“之前,该警报会阻止对页面进行进一步操作”;“确定”;。请参见
original answer 我在哪里找到了这个解决方案
another like it. 以下是我的代码:
// Validate Teams and Team News Posts Before Publishing
// modified from https://wordpress.stackexchange.com/a/42709/127459
add_action(\'admin_head-post.php\',\'ep_publish_admin_hook\');
add_action(\'admin_head-post-new.php\',\'ep_publish_admin_hook\');
function ep_publish_admin_hook(){
global $post;
if ( is_admin() && ($post->post_type == \'team\' || $post->post_type == \'team-news\') ){
?>
<script language="javascript" type="text/javascript">
jQuery(document).ready(function() {
console.log(\'running this now\');
jQuery(\'#publish\').click(function() {
if(jQuery(this).data("valid")) {
return true;
}
var form_data = jQuery(\'#post\').serializeArray();
var data = {
action: \'ep_pre_submit_validation\',
security: \'<?php echo wp_create_nonce( \'pre_publish_validation\' ); ?>\',
form_data: jQuery.param(form_data),
};
jQuery.post(ajaxurl, data, function(response) {
if (response.indexOf(\'true\') > -1 || response == true) {
jQuery("#post").data("valid", true).submit();
} else {
alert("Error: " + response);
jQuery("#post").data("valid", false);
}
//hide loading icon, return Publish button to normal
jQuery(\'#ajax-loading\').hide();
jQuery(\'#publish\').removeClass(\'button-primary-disabled\');
jQuery(\'#save-post\').removeClass(\'button-disabled\');
});
return false;
});
});
</script>
<?php
}
}
add_action(\'wp_ajax_ep_pre_submit_validation\', \'ep_pre_submit_validation\');
function ep_pre_submit_validation() {
//simple Security check (checks for post-type team or team-news, etc, via scripts above in ep_publish_admin_hook)
check_ajax_referer( \'pre_publish_validation\', \'security\' );
//convert the string of data received to an array
//from https://wordpress.stackexchange.com/a/26536/10406
parse_str( $_POST[\'form_data\'], $vars );
// _e(print_r($vars));
//check that are actually trying to publish a post
if ( $vars[\'post_status\'] == \'publish\' ||
(isset( $vars[\'original_publish\'] ) &&
in_array( $vars[\'original_publish\'], array(\'Publish\', \'Schedule\', \'Update\') ) ) ) {
// Check that post title is set
if (!isset($vars[\'post_title\'])) {
// _e(print_r($vars)); // uncomment to see what is included in this array
_e(\'Please provide a post title.\');
die();
}
// Check that gender is set
if (!isset($vars[\'gender\'])) {
// _e(print_r($vars)); // uncomment to see what is included in this array
_e(\'Please specify a gender.\');
die();
}
}
//everything ok, allow submission
echo \'true\';
die();
}
注意:这两种解决方案都适用于我,但在我的实现中,解决方案“A”需要在进行更改后重新加载浏览器以更新错误消息(例如,如果返回并修复所有错误,则“发布”按钮仍会触发错误页,但当您重新加载页面时,它会发布帖子)。于是我找到了解决方案B,并根据自己的需要对其进行定制,对多种帖子类型进行多次验证。