防止作者发布过短的内容:
例如,这里有一个使用自定义帖子状态的想法
short
:
/**
* Register a custom \'short\' post status
*
* @see http://wordpress.stackexchange.com/a/159044/26350
*/
function wpse_short_post_status()
{
register_post_status( \'short\', array(
\'label\' => _x( \'Short\', \'post\' ),
\'public\' => false,
\'exclude_from_search\' => true,
\'show_in_admin_all_list\' => true,
\'show_in_admin_status_list\' => true,
\'label_count\' => _n_noop( \'Short <span class="count">(%s)</span>\',
\'Short <span class="count">(%s)</span>\' )
) );
}
add_action( \'init\', \'wpse_short_post_status\' );
然后,我们可以在此处查看所有内容太短的帖子:
/wp-admin/edit.php?post_status=short&post_type=post
使用额外选项卡:
为了防止作者发布内容过短的帖子,我们可以使用wp_insert_post_data
过滤器:
/**
* Prevent authors from publishing posts with too short content.
*
* @see http://wordpress.stackexchange.com/a/159044/26350
*/
function wpse_prevent_short_content( $data , $postarr )
{
// Editors and admins can publish all posts:
if( current_user_can( \'edit_others_posts\' ) )
return $data;
// Authors can\'t publish posts with too short content:
$wordcount = count( explode( \' \', strip_tags( $data[\'post_content\'] ) ) );
if( \'publish\' === $data[\'post_status\'] && $wordcount <= 250 )
$data[\'post_status\'] = \'short\';
return $data;
}
add_filter( \'wp_insert_post_data\', \'wpse_prevent_short_content\', PHP_INT_MAX, 2 );
在发布时,我们强制将post状态返回到short。
我们可以使用它来警告用户内容太短:
/**
* Display a too short content warning.
*
* @see http://wordpress.stackexchange.com/a/159044/26350
*/
function wpse_admin_notice() {
$screen = get_current_screen();
if( \'post\' === $screen->base
&& \'post\' === $screen->id
&& \'short\' === $GLOBALS[\'post\']->post_status
&& ! current_user_can( \'edit_others_posts\' )
)
{
printf( \'<div class="error"><p>%s</p></div><style>#message{display:none;}</style>\',
__( \'Warning: Post not published - the content must exceed 250 words!\' )
);
}
}
add_action( \'admin_notices\', \'wpse_admin_notice\' );
以下是警告的屏幕截图:
我希望您可以根据自己的需要对其进行修改,例如,如果您需要将其用于除post之外的其他职位类型。