我想让其他人在我的WordPress网站上发布帖子,但我不希望他们能够在管理区域(或其他任何地方)编辑帖子日期。有没有办法禁用此功能?理想情况下,WordPress应该从服务器获取当前时间并将其插入数据库中以获取初始帖子(未接受用户输入),并且不能从该点进行修改。
删除CSS中的编辑链接不是我想要的,我需要在其核心禁用此功能。
我想过使用wp_insert_post_data过滤器,但我很快发现这可以通过快速编辑来绕过,而且似乎有太多我没想过的陷阱。
function insert_post_data_handler( $data , $postarr ) {
if ( $data['post_status'] === 'auto-draft' ){
// Do Nothing, WordPress Does not generate date from POST/GET request
} elseif ( $data['post_status'] !== 'publish' ){
// Must be a draft, so automatically make the post date the current date/time
$datetime = date("Y-m-d H:i:s");
$data['post_date'] = $datetime;
$data['post_date_gmt'] = $datetime;
$data['post_modified'] = $datetime;
$data['post_modified_gmt'] = $datetime;
} elseif ( $data['post_status'] === 'publish' ){
// Could either be an update to a post, or a brand new post
if ( isset( $postarr['original_post_status'] ) ){
if( $postarr['original_post_status'] !== 'publish' && $data['post_status'] === 'publish' ){
// This is a brand new post
$datetime = date("Y-m-d H:i:s");
$data['post_date'] = $datetime;
$data['post_date_gmt'] = $datetime;
} else {
// This must be an update, do not change the date
global $post;
$org_datetime = $post->post_date;
$current_datetime = date("Y-m-d H:i:s");
$data['post_date'] = $org_datetime;
$data['post_date_gmt'] = $org_datetime;
$data['post_modified'] = $current_datetime;
$data['post_modified_gmt'] = $current_datetime;
}
} elseif( $postarr['original_post_status'] === 'publish' && $data['post_status'] !== 'publish' ){
// Sorry, not allowing anyone to revert to draft/pending once published
die('The status can not be reverted once posted');
}
} else {
die('Uncaught exception');
}
return $data;
}
add_filter( 'wp_insert_post_data', 'insert_post_data_handler', '99', 2 );