...

/

Per User Post Limit

Per User Post Limit

Learn to put a cap on the number of posts created by a user.

Cap on user generated content

A malicious activity on the website may result in a user with teacher role creating millions of lecture posts in an attempt to fill up our database or make the website slow. We will restrict user accounts from creating a limited number of posts.

Let's limit the teacher role to have 5 posts at the max to demonstrate the concept.

Open the functions.php file. We have a filter that intercepts requests right before data gets saved into the database. We can use the same filter to ensure the max post limit.

The count_user_posts is a WordPress function that counts the total number of posts authored by a particular user based on their user ID and post type. It takes two arguments.

  1. The first is the user ID from get_current_user_id() function.

  2. The second argument is the post type we are trying to count like post, teacher, course or event etc. We want to count lecture posts.

If the condition is true, we will send an error message ...