Limit number of posts a user can make per minute?

So I found the solution to limiting a number of posts a user can make per day but not per minute.

Here’s an excerpt from the code I found on http://wordpress.org/support/topic/limit-the-number-of-posts-a-user-can-make?replies=18

Read More
    //limit the quantity to N posts
    $N = 20; max 20 posts por usuario
    $count_posts = count(get_posts(array('author'=>$user_ID)));// returns the quantity of written posts by current user to $user_ID
    if ($count_posts >= N){
    Header("Location: index.php");//redirects to dashboard, where it should point the user that he cannot create a new post
    }
    //end of modification

How can I modify the $count_posts var so that it counts time? I want to make it so that users can only submit one post every 2 minutes. If they submit a post in the server, they have to wait 2 more minutes before submitting another post.

Related posts

1 comment

  1. I would begin with establishing the most friendly approach to this. I think it would be a warning after post Publish/Update button is clicked. It is possible to prevent the Edit Post screen loading if the user re-visits it within 2 minutes after the previous post is published. I don’t think that is practical though and I don’t see why the redirect needs to happen. How about just not allowing the publish to complete but allow users to create drafts?

    Do the check during publishing and unless your under attack as already mentioned then very few users will ever see any type of warning…

    add_action( "publish_post", 'limit_post_frequency' );
    
    function limit_post_frequency(){
        /* here you could store the time() in user meta, if no existing value found.
           If the time() meta value already exists, check if it is 120 seconds in the past.
           If 120 seconds in the past, update the user meta value with time()
           Else
           Do a WP notice letting user know they need to wait, you could calculate how
           long they have to wait.
        */
    }
    

    If using my approach. I would consider storing an array in the user meta value and including a count of attempts. If user attempts to submit too many times you could treat it as a security issue and deal with it, automatically.

    UPDATE: I would like to add a note regarding security. If the limitations are with spammers in mind, then they could create a lot of drafts using my approach. A solution to delete drafts automatically probably exists. It would be easy enough to send email reminders to authors to warn them that a draft will expire soon and maybe even send them a copy of their post in email while deleting it.

    Also keep in mind that posts can be scheduled. So your authors could create drafts after reaching their daily limit of say 20, but create drafts that are scheduled and automatically published. I think I would enjoy creating a plugin that handles this so I’ve added it to my to do list.

Comments are closed.