Last active
June 6, 2016 19:24
-
-
Save jimi008/708f32b9b739cc2a56aa695abb84a4bd to your computer and use it in GitHub Desktop.
Prevent authors from publishing too short content
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
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' ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
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 ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Display a too short content warning. | |
* | |
* | |
*/ | |
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' ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/wp-admin/edit.php?post_status=short&post_type=post |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment