代码将过滤器添加到wp_insert_post_data
:
add_filter(\'wp_insert_post_data\', \'mandatory_excerpt\');
下面是回调:
function mandatory_excerpt($data) {
$excerpt = $data[\'post_excerpt\'];
if (empty($excerpt)) {
if ($data[\'post_status\'] === \'publish\') {
add_filter(\'redirect_post_location\', \'excerpt_error_message_redirect\', \'99\');
}
$data[\'post_status\'] = \'draft\';
}
return $data;
}
筛选器回调已通过
$data
, 哪一个
as per the Codex 包括以下post数据:
\'post_author\',
\'post_date\',
\'post_date_gmt\',
\'post_content\',
\'post_content_filtered\',
\'post_title\',
\'post_excerpt\',
\'post_status\',
\'post_type\',
\'comment_status\',
\'ping_status\',
\'post_password\',
\'post_name\',
\'to_ping\',
\'pinged\',
\'post_modified\',
\'post_modified_gmt\',
\'post_parent\',
\'menu_order\',
\'guid\'
这些数据包括
\'post_type\'
, 这意味着您可以在回调中使用它:
function mandatory_excerpt($data) {
if ( \'custom-posttype-slug\' != $data[\'post_type\'] ) {
return $data;
} else {
$excerpt = $data[\'post_excerpt\'];
if (empty($excerpt)) {
if ($data[\'post_status\'] === \'publish\') {
add_filter(\'redirect_post_location\', \'excerpt_error_message_redirect\', \'99\');
}
$data[\'post_status\'] = \'draft\';
}
}
return $data;
}