WordPress.org

Ready to get started?Download WordPress

Forums

Seeking simple (ish) code to limit posts per day (4 posts)

  1. SpikeTheLobster
    Member
    Posted 1 year ago #

    Hi,

    Given that there's no simple plugin to do this job, I'm looking for a way to add a few lines of code to a theme's "post" page (whatever that may be) to check whether a user has posted a given number of posts in a day.

    i.e. If they've posted 5 entries (post_type='post') in the last day, they can't post any more.

    I don't need any flexibility beyond that and I don't want to mess around with unnecessarily complex hooks, actions, blah blah blah. Just a few simple lines of code.

    I've tried a few attempts but my coding skills are, well... iffy at best. :)

    Anyone who can help me out here?

  2. bcworkz
    Member
    Posted 1 year ago #

    There's a number of approaches. One would be to query the database for a count of posts by the current user in a date range from 24 hours ago to now. If the result is >= 5, die() with a message saying they've reached their max. quota.

    The real trick is figuring out where to put this code. It won't be a theme template, but a hook in the theme's functions.php might work. Or a hook from a plugin.

    Oh, you don't like hooks. Well that's the only way to properly extend WP functionality. As it happens, there doesn't seem to be a good hook to use anyway. If you're going to do a core hack despite warnings not to, just find where the add new post code starts and stick your code there.

  3. Arevico
    Member
    Posted 1 year ago #

    you van use the save_post hook, then do query_post http://codex.wordpress.org/Function_Reference/query_posts and limit by date. Check if the count is not higher than x. That should work i think.

    You can put the code in a new plugin. I'm afraid you'll have to get your hand dirty on some code, but it isn't that hard to be honest. Just follow the excelent guides on this website and use php.net.

  4. SpikeTheLobster
    Member
    Posted 1 year ago #

    "Oh, you don't like hooks. Well that's the only way to properly extend WP functionality."

    I don't want to properly extend it. I want to add a simple check, rather than invest hours of time in learning a pile of stuff I'm unlikely to ever use again.

    "You can put the code in a new plugin. I'm afraid you'll have to get your hand dirty on some code, but it isn't that hard to be honest."

    It is when you have to start from scratch and learn all the "how to code for WP" stuff - the time investment is absolutely massive for one simple check, which is precisely what I want to avoid.

    Ah, well. I guess I'll just live with it for the moment - at least until I have hours of spare time to put into all the learning (and reading that awful codex).

    Thanks for the input, guys.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.