Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

    //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.

share|improve this question
1  
That code does not limit posts per day. It is a global limit, and it has syntax errors. –  s_ha_dum Oct 24 '13 at 9:49
1  
I doubt that humans users could type and click quickly enough and often enough for two minute intervals to be a worry so I am assuming that you have some kind of "front-end posting" that is being abused by spam-bots, correct? If so, post that code. –  s_ha_dum Oct 24 '13 at 9:55
    
Why look for an already written solution? Write one yourself. Add a last_post_time field with the time stamp of the last post to user meta and check it when a post is added in less than a 2 minute interval. –  Charles Clarkson Oct 24 '13 at 16:09
    
@s_ha_dum - You'd be surprised how quickly it takes to find an image, write a small caption for it and then upload it. My website is a meme site, not an article site. –  user1114968 Oct 25 '13 at 7:46
1  
Please note that editing core files is considered poor and problematic practice. I like this question in general form, however you might have trouble getting answers if you have no skill with PHP to provide code context and feedback. –  Rarst Dec 12 '13 at 15:47

1 Answer 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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.