User Posts Limit

How to set post creation limit for authors?
In the plugin settings set ‘Author’ in role, ‘Posts’ in post type and the limit.

How to make rules that applied on certain post type to limit per website instead of per user role?
Update the post type:

add_filter( 'upl_query', 'upl_limit_total_posts' );
add_filter( 'upl_rule_limit_current_user_role_check', 'upl_convert_user_role', 10, 2 );

function upl_limit_total_posts( $args ) {
	$post_type = 'post';
	if ( $post_type === $args['post_type'] ) {
		unset( $args['author'] );
	}
	return $args;
}

function upl_convert_user_role( $check, $i ) {
	$post_type = 'post';
	return $post_type === get_option( 'upl_posts_type' )[ $i ] && ! is_user_logged_in() ? true : $check;
}

How to make rules that applied on certain post type to limit by posts in specific categories only?
Update the post type and the categories id (make sure users can’t migrate posts from/into unrestricted posts categories):

add_filter( 'upl_query', 'upl_specific_category' );
function upl_specific_category( $args ) {
	$post_type = 'post';
	$category_id = [ '4', '2' ];
	if ( $post_type === $args['post_type'] ) {
		$args['cat'] = $category_id;
	}
	return $args;
}

How to make rules that applied on certain post type to limit only specific post statuses?
Update the post type and the post statuses (make sure users can’t migrate posts from/into unrestricted post status):

add_filter( 'upl_query', 'upl_specific_post_status' );
function upl_specific_post_status( $args ) {
	$post_type = 'post';
	$post_status = [ 'any' ];
	if ( $post_type === $args['post_type'] ) {
		$args['post_status'] = $post_status;
	}
	return $args;
}

How to modify the cycle for rules that applied on certain post type?
Update the post type and the cycle:

add_filter( 'upl_query', 'upl_modify_cycle' );
function upl_modify_cycle( $args ) {
	$post_type = 'post';
	$cycle = '3 days ago';
	if ( $post_type === $args['post_type'] ) {
		$args['date_query']['after'] = $cycle;
	}
	return $args;
}

How to display to the users only the limits that applied on them based on the selected ‘Priority’ option?

add_filter( 'upl_display_prioritized_rules_enabled', '__return_true' );

How to prevent users to exceed the limits by opening multiple ‘new post’ windows?

add_filter( 'upl_skip_auto_draft_enabled', '__return_true' );

How to hide the ‘Add New’ button when posts limit exceeded?

add_filter( 'upl_hide_add_new_button_enabled', '__return_true' );

How to define limits for the total number of multiple post types?
Update the packages’ names and the post types they include:

add_action( 'init', 'upl_create_packages' );
add_action( 'admin_init', 'upl_add_author_support' );
add_filter( 'upl_rule_limit_current_post_type_check', 'upl_convert_post_types', 10, 3 );
add_filter( 'upl_query', 'upl_apply_packages', 10, 2 );

function upl_get_packages_types() {
	return [
		'Basic Package' => [ 'post', 'page' ],
		'Premium Package' => [ 'post', 'page', 'media' ]
	];
}

function upl_create_packages() {
	foreach ( array_keys( upl_get_packages_types() ) as $package ) {
		register_post_type( sanitize_key( $package ), [ 'labels' => [ 'name' => $package ] ] );
	}
}

function upl_add_author_support() {
	if ( current_user_can( get_option( 'upl_manage_cap' ) ) ) {
		$post_types = [];
		foreach ( upl_get_packages_types() as $package ) {
			$post_types = array_merge( $post_types, $package );
		}
		foreach ( $post_types as $post_type ) {
			add_post_type_support( $post_type, 'author' );
		}
	}
}

function upl_convert_post_types( $check, $i, $current_post_type ) {
	foreach ( upl_get_packages_types() as $package => $types ) {
		if ( sanitize_key( $package ) === get_option( 'upl_posts_type' )[ $i ] && in_array( $current_post_type, $types ) ) {
			return true;
		}
	}
	return $check;
}

function upl_apply_packages( $args, $i ) {
	foreach ( upl_get_packages_types() as $package => $types ) {
		if ( sanitize_key( $package ) === get_option( 'upl_posts_type' )[ $i ] ) {
			$args['post_type'] = $types;
			break;
		}
	}
	return $args;
}

How to lock the edit of posts for some user role when certain condition are met?
Update the post type, the role, and the condition:

add_filter( 'get_post_metadata', 'upl_lock_posts', 10, 5 );
function upl_lock_posts( $value, $object_id, $meta_key, $single, $meta_type ) {
	$post_type = 'post';
	$role = 'editor';
	$condition = 'active' !== get_user_meta( get_current_user_id(), 'membership_level', true );
	$admin_id = '1';
	return '_edit_lock' === $meta_key && $post_type === get_post_type( $object_id ) && in_array( $role, wp_get_current_user()->roles ) && $condition ? time() . ':' . $admin_id : $value;
}

How to make rules that apply on certain post type to limit by the posts of the current user instead of the assigned owner (suitable for frontend forms that create the post as admin and are modified only afterwards)?
Update the post type:

add_filter( 'upl_query', 'upl_limit_current_user' );
function upl_limit_current_user( $args ) {
	$post_type = 'post';
	if ( $post_type === $args['post_type'] && ! is_admin() ) {
		$args['author'] = get_current_user_id();
	}
	return $args;
}

How to allow posts limit per user (will be displayed on the Edit User Screen)?

add_filter( 'upl_rule_num_limit', 'upl_user_num_limit', 10, 3 );
add_action( 'show_user_profile', 'upl_add_num_limit_user_data' );
add_action( 'edit_user_profile', 'upl_add_num_limit_user_data' );
add_action( 'personal_options_update', 'upl_save_num_limit_user_data' );
add_action( 'edit_user_profile_update', 'upl_save_num_limit_user_data' );
add_filter( 'manage_users_columns', 'upl_num_limit_user_table' );
add_filter( 'manage_users_custom_column', 'upl_num_limit_user_table_row', 10, 3 );

function upl_user_num_limit( $num_limit, $i, $user ) {
	$user_limit = get_user_meta( $user, 'num_limit', true );
	return '' !== $user_limit ? $user_limit : $num_limit;
}

function upl_add_num_limit_user_data( $user ) {
	if ( current_user_can( get_option( 'upl_manage_cap', 'manage_options' ) ) ) {
		?>
		<h3><?php esc_html_e( 'User Posts Limit', 'user-posts-limit' ); ?></h3>
		<table class="form-table">
			<tr>
				<th><label for="num_limit"><?php esc_html_e( 'Limit', 'user-posts-limit' ); ?></label></th>
				<td>
					<input type="number" min="0" name="num_limit" value="<?php echo esc_attr( get_user_meta( $user->ID, 'num_limit', true ) ); ?>" class="regular-text" />
					<p class="description"><?php esc_html_e( 'It will override the limit of all the rules that applies on this user', 'user-posts-limit' ); ?></p>
				</td>
			</tr>
		</table>
		<br>
		<?php
	}
}

function upl_save_num_limit_user_data( $user_id ) {
	if ( current_user_can( get_option( 'upl_manage_cap', 'manage_options' ) ) ) {
		update_user_meta( $user_id, 'num_limit', sanitize_text_field( $_POST['num_limit'] ) );
	}
}

function upl_num_limit_user_table( $columns ) {
	$columns['num_limit'] = __( 'Limit', 'user-posts-limit' );
	return $columns;
}

function upl_num_limit_user_table_row( $row_output, $column_id_attr, $user_id ) {
	return 'num_limit' === $column_id_attr ? get_user_meta( $user_id, 'num_limit', true ) : $row_output;
}

71 responses to “User Posts Limit”

  1. Louis Avatar
    Louis

    Bonjour,

    Voici ma problématique.

    J’ai un formulaire qui permet à l’utilisateur de créer un post qui obtient le statut “En attente de relecture”.

    Si celui-ci est validé (par l’administrateur), il passe alors en publié. Le problème est le suivant : l’utilisateur peut décider de dépublier son article qui passe dans avec le statut “draft”. Cependant, il peut également décider de le republier avec le statut “publish”.

    Mon problème est donc que cela ne fait pas référence à une création de post mais à un changement de statut. Est-il possible de simplement limiter la création de posts, et la mise à jour du staut a “publish” ?

    Merci pour votre aide et votre merveilleux plugin.

    1. Condless Avatar

      Hi Louis,
      The user ‘republish’ the post via the frontend or the backend?
      Try using the code from ‘How to make rules that applied on certain post type to limit specific post statuses?’ section to limit only ‘publish’ posts and wrap the Publish button of your form with the [upl_hide] shortcode, so the button will be hidden once the user excceed the published posts limit.

    2. Louis Avatar
      Louis

      The user republishes the article from the front-end via a form from the Crocoblock plugin: JetFormBuilder.

      The problem is that I am not able to hide the button with the shortcode.

      Also, I tried to display the shortcode announcing the number of posts the user is able to publish on the front-end, but it doesn’t work.

    3. Condless Avatar

      Hi,
      Insert 2 Shortcode widgets using Elementor, before the button:
      [upl_hide type="post"][/upl_start]
      after the button:
      [/upl_hide]

  2. Maria Avatar

    Hello,
    Is it possible to notify a user on the frontend that the post limit has been exceeded?
    Thank you

    1. Condless Avatar

      Hi Maria,
      Try the shortcode (replace “post” with the relevant post type):
      [upl_hide type="post"]

    2. Maria Avatar

      Hello,

      I have added the shortcode [upl_hide type=”videos”] but it doesn’t show anything to the user on the front page.

      I have a limit so any user with the “subscriber” role cannot upload more than ten videos and it works.The problem is that it doesn’t notify you that you have exceeded the limit.

      Thank you a lot

    3. Condless Avatar

      Hi Maria,
      Try “video” instead “videos”.

    4. Maria Avatar

      Hello,
      Yes, yesterday I tried both options but it was still the same

    5. Condless Avatar

      Hi,
      Insert 2 Shortcode widgets using Elementor, the first contains:
      [upl_hide type="video"][/upl_start]
      and the second contains:
      [/upl_hide]

  3. Unakriti Avatar
    Unakriti

    Thank you so much for this plugin.

    I am trying to control for a CPT made with Metabox.io. But it does not work – the restricted user-role can continue to create more posts. There are no warnings / limit notifications shown either.

    What am I missing?

    Really appreciate this plugin and sincerely hope I can make it work to my scenario.

    Kind regards,

    1. Condless Avatar

      Hi,
      The plugin should work on CPT as well, a detailed answer was given by Email.

  4. joko Avatar
    joko

    how integrate with plugin membership, any hooks and filter doc? thanks

    1. Condless Avatar

      Hi Joko,
      Integrations are available for WooCommerce, Paid Membership Pro, MyListing theme, Restrict Content Pro. Which membership plugin do you use?

  5. Fachrul Hasan Avatar
    Fachrul Hasan

    Hello, how can I show how much time left customer has on the front end?

    1. Condless Avatar

      Hi Fachrul,
      In the ‘text’ option (via the plugin settings) insert ‘{release_date}’, then in the relevant page use the shortcode (replace “post” with the relevant post type): [upl_hide type="post"]

  6. אהרן כהן Avatar
    אהרן כהן

    Hello, is it possible to separate the number of posts the user has published and the number of posts he can publish so that the shortcode will not display both

    1. Condless Avatar

      Hi Aaron,
      You can add the ‘format’ attribute to the shortcode as follow:
      [upl_limits format= "{type} limit: {limit}. "]

    2. אהרן כהן Avatar
      אהרן כהן

      Hi thank you very much you helped me a lot only shortcode shows only how much I can post
      I also need a shortcode of how much the user posted

    3. Condless Avatar

      You can use {left} or {count} instead of {limit}

  7. MJ Avatar
    MJ

    I used your plugin and a problem occurred~

    When the set condition is triggered, a wordpress error will pop up

    How can I solve this problem?

    If you receive a message, I hope you can reply me~

    1. Condless Avatar

      Hi MJ,
      Users submit posts using the admin dashboard or using some plugin? can you provide link to the website?

  8. MJ Avatar
    MJ

    Hello,Condless~

    I emailed you~

    In the email, there are many pictures~

  9. MJ Avatar
    MJ

    Hello! I have solved this problem!

    On line 576 of the code:

    wp_ die( $prepared_message ,”, [ ‘back_link’ => true ] );

    There is no content in ”

    So a wordpress error is displayed

    So I changed it to:

    wp_ die( $prepared_message ,’ Prompt’, [ ‘back_link’ => true ] );

    And solved the problem

    Very good plug-in, it helped me solve the problem

    Thank you.

Leave a Reply

Your email address will not be published. Required fields are marked *