This code will limit the number of posts a user can make.
At first, I was trying to limit each user to one post and I had that working. Then I realized that I needed to have different numbers of posts for different users, so I created an option in a user’s profile that only people with the right capability (manage_options) can edit. I’ll be changing this in the future for that number to be automatically changed depending on a user’s subscription type, but I needed this for testing purposes.
The option in the profile shows up under the contact info section, which I know is an odd place, but it was the easiest place to put it.
Put the following into your functions.php file:
// limit number of posts per user
// provided by cleverness.org
if ( !current_user_can('manage_options') ) {
$default = 1; // default number of posts
$count_posts = 0;
global $wpdb;
$poststable = $wpdb->prefix.'posts';
$postlimit = get_user_meta($current_user->id, 'postlimit', true);
if ( $postlimit == '' ) $postlimit = $default;
$query = "SELECT COUNT(*) FROM $poststable WHERE post_author = '$current_user->id' AND (post_status = 'pending' OR post_status = 'draft' OR post_status = 'publish' ) ";
$count_posts = $wpdb->get_var($query);
if ($count_posts >= $postlimit) {
if ( $_SERVER['REQUEST_URI'] == '/wp-admin/post-new.php' )
Header("Location: index.php");//redirects to dashboard
if ( is_admin() ){
$stylesheet = get_stylesheet_directory_uri() . '/css/limitposts.css';
wp_register_style('limitpost_admin_css', $stylesheet, false, '1', 'screen');
wp_enqueue_style('limitpost_admin_css');
}
}
}
// add post limit option to profile
add_filter('user_contactmethods','hide_profile_fields',10,1);
function hide_profile_fields( $contactmethods ) {
if( current_user_can( 'manage_options' ) )
$contactmethods['postlimit'] = 'Post Limit';
return $contactmethods;
}
Create a file called limitposts.css, put the following into it, and save it in a directory called css in your theme folder (you can change where you save it, but be sure to change the location in functions.php).
#menu-posts .wp-first-item + li {
display: none;
}
If you want to use this for custom post types, change:
if ( $_SERVER['REQUEST_URI'] == '/wp-admin/post-new.php' )
to:
if ( $_SERVER['REQUEST_URI'] == '/wp-admin/post-new.php?post_type=customposttype' )
and change the id name in your CSS:
#menu-posts-customposttype .wp-first-item + li {
display: none;
}
Be sure to replace customposttype with the name of your custom post type.