问题描述
我有一个自定义的帖子类型 Event
包含一个开始和结束日期/时间自定义字段 (作为后缀编辑屏幕中的 metaboxes) 。
我想确保一个事件不会被发布 (或安排),而没有填写日期,因为这将导致显示事件数据的模板的问题 (除了事实,这是一个必要的要求!) 。但是,我希望能够在准备中不包含有效日期的草稿事件。
我正在想挂钩 save_post
进行检查,但是如何防止状态更改发生?
EDIT1:这是我现在用来保存 post_meta 的钩子。
// Save the Metabox Data
function ep_eventposts_save_meta( $post_id, $post ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
if ( !isset( $_POST['ep_eventposts_nonce'] ) )
return;
if ( !wp_verify_nonce( $_POST['ep_eventposts_nonce'], plugin_basename( __FILE__ ) ) )
return;
// Is the user allowed to edit the post or page?
if ( !current_user_can( 'edit_post', $post->ID ) )
return;
// OK, we're authenticated: we need to find and save the data
// We'll put it into an array to make it easier to loop though
//debug
//print_r($_POST);
$metabox_ids = array( '_start', '_end' );
foreach ($metabox_ids as $key ) {
$events_meta[$key . '_date'] = $_POST[$key . '_date'];
$events_meta[$key . '_time'] = $_POST[$key . '_time'];
$events_meta[$key . '_timestamp'] = $events_meta[$key . '_date'] . ' ' . $events_meta[$key . '_time'];
}
$events_meta['_location'] = $_POST['_location'];
if (array_key_exists('_end_timestamp', $_POST))
$events_meta['_all_day'] = $_POST['_all_day'];
// Add values of $events_meta as custom fields
foreach ( $events_meta as $key => $value ) { // Cycle through the $events_meta array!
if ( $post->post_type == 'revision' ) return; // Don't store custom data twice
$value = implode( ',', (array)$value ); // If $value is an array, make it a CSV (unlikely)
if ( get_post_meta( $post->ID, $key, FALSE ) ) { // If the custom field already has a value
update_post_meta( $post->ID, $key, $value );
} else { // If the custom field doesn't have a value
add_post_meta( $post->ID, $key, $value );
}
if ( !$value )
delete_post_meta( $post->ID, $key ); // Delete if blank
}
}
add_action( 'save_post', 'ep_eventposts_save_meta', 1, 2 );
EDIT2:这是我在保存到数据库后试图用来检查帖子数据。
add_action( 'save_post', 'ep_eventposts_check_meta', 99, 2 );
function ep_eventposts_check_meta( $post_id, $post ) {
//check that metadata is complete when a post is published
//print_r($_POST);
if ( $_POST['post_status'] == 'publish' ) {
$custom = get_post_custom($post_id);
//make sure both dates are filled
if ( !array_key_exists('_start_timestamp', $custom ) || !array_key_exists('_end_timestamp', $custom )) {
$post->post_status = 'draft';
wp_update_post($post);
}
//make sure start < end
elseif ( $custom['_start_timestamp'] > $custom['_end_timestamp'] ) {
$post->post_status = 'draft';
wp_update_post($post);
}
else {
return;
}
}
}
主要问题是 another question 中实际描述的一个问题:save_post
中的 wp_update_post()
钩子触发无限循环。
EDIT3:我想到了一种方法,通过挂接 wp_insert_post_data
而不是 save_post
。唯一的问题是现在 post_status
被恢复,但现在有一个误导性的消息,表示”Post published” 显示 (通过将&message=6
添加到重定向的 URL),但状态设置为草稿。
add_filter( 'wp_insert_post_data', 'ep_eventposts_check_meta', 99, 2 );
function ep_eventposts_check_meta( $data, $postarr ) {
//check that metadata is complete when a post is published, otherwise revert to draft
if ( $data['post_type'] != 'event' ) {
return $data;
}
if ( $postarr['post_status'] == 'publish' ) {
$custom = get_post_custom($postarr['ID']);
//make sure both dates are filled
if ( !array_key_exists('_start_timestamp', $custom ) || !array_key_exists('_end_timestamp', $custom )) {
$data['post_status'] = 'draft';
}
//make sure start < end
elseif ( $custom['_start_timestamp'] > $custom['_end_timestamp'] ) {
$data['post_status'] = 'draft';
}
//everything fine!
else {
return $data;
}
}
return $data;
}
最佳解决方案
正如 m0r7if3r 所指出的那样,没有办法使用 save_post
钩子来发布一个帖子,因为当钩子被触发时,该帖子已被保存。然而,以下内容将允许您在不使用 wp_insert_post_data
的情况下恢复状态,而不会导致无限循环。
以下没有测试,但应该工作。
<?php
add_action('save_post', 'my_save_post');
function my_save_post($post_id) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
if ( !isset( $_POST['ep_eventposts_nonce'] ) )
return;
if ( !wp_verify_nonce( $_POST['ep_eventposts_nonce'], plugin_basename( __FILE__ ) ) )
return;
// Is the user allowed to edit the post or page?
if ( !current_user_can( 'edit_post', $post->ID ) )
return;
// Now perform checks to validate your data.
// Note custom fields (different from data in custom metaboxes!)
// will already have been saved.
$prevent_publish= false;//Set to true if data was invalid.
if ($prevent_publish) {
// unhook this function to prevent indefinite loop
remove_action('save_post', 'my_save_post');
// update the post to change post status
wp_update_post(array('ID' => $post_id, 'post_status' => 'draft'));
// re-hook this function again
add_action('save_post', 'my_save_post');
}
}
?>
我没有检查,但是看代码,反馈消息将显示该帖子发布的错误消息。这是因为 WordPress 将我们重定向到了 message
变量现在不正确的 URL 。
要更改它,我们可以使用 redirect_post_location
过滤器:
add_filter('redirect_post_location','my_redirect_location',10,2);
function my_redirect_location($location,$post_id){
//If post was published...
if (isset($_POST['publish'])){
//obtain current post status
$status = get_post_status( $post_id );
//The post was 'published', but if it is still a draft, display draft message (10).
if($status=='draft')
$location = add_query_arg('message', 10, $location);
}
return $location;
}
总结上述重定向过滤器:如果一个帖子设置为发布,但仍然是草稿,那么我们相应地更改消息 (这是 message=10
) 。再次,这是未经测试,但应该工作。 add_query_arg
的 Codex 表明,当已经设置了一个变量时,该函数将替换它 (但是如我所说,我还没有测试过) 。
次佳解决方案
好的,这是最后我怎么做到这一点:一个 Ajax 调用一个 PHP 函数进行检查,这种灵感来自于 this answer,并使用了一个来自 question I asked on StackOverflow 的一个聪明的提示。重要的是,我确保只有当我们想要发布检查完成时,才能始终保存草稿,而无需检查。这最终成为实际阻止该职位发布的更容易的解决方案。它可能会帮助别人,所以我写在这里。
首先,添加必要的 Javascript:
//AJAX to validate event before publishing
//adapted from https://wordpress.stackexchange.com/questions/15546/dont-publish-custom-post-type-post-if-a-meta-data-field-isnt-valid
add_action('admin_enqueue_scripts-post.php', 'ep_load_jquery_js');
add_action('admin_enqueue_scripts-post-new.php', 'ep_load_jquery_js');
function ep_load_jquery_js(){
global $post;
if ( $post->post_type == 'event' ) {
wp_enqueue_script('jquery');
}
}
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 == 'event' ){
?>
<script language="javascript" type="text/javascript">
jQuery(document).ready(function() {
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
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 );
//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') ) ) ) {
if ( empty( $vars['_start_date'] ) || empty( $vars['_end_date'] ) ) {
_e('Both Start and End date need to be filled');
die();
}
//make sure start < end
elseif ( $vars['_start_date'] > $vars['_end_date'] ) {
_e('Start date cannot be after End date');
die();
}
//check time is also inputted in case of a non-all-day event
elseif ( !isset($vars['_all_day'] ) ) {
if ( empty($vars['_start_time'] ) || empty( $vars['_end_time'] ) ) {
_e('Both Start time and End time need to be specified if the event is not an all-day event');
die();
}
elseif ( strtotime( $vars['_start_date']. ' ' .$vars['_start_time'] ) > strtotime( $vars['_end_date']. ' ' .$vars['_end_time'] ) ) {
_e('Start date/time cannot be after End date/time');
die();
}
}
}
//everything ok, allow submission
echo 'true';
die();
}
如果一切正常,该函数返回 true
,并提交表单以通过普通通道发布帖子。否则,该函数返回一个显示为 alert()
的错误消息,并且表单未提交。
参考文献
注:本文内容整合自 Google/Baidu/Bing 辅助翻译的英文资料结果。如果您对结果不满意,可以加入我们改善翻译效果:薇晓朵技术论坛。