Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Pay Per Post for WordPress with Paid Memberships Pro
Limiting posts per user.
Enable the Paid Memberships Pro plugin and then add this code to your functions.php or as a stand alone plugin.
Be sure to read through this code carefully. Update each function to suit your needs.
This code has not been tested. Feel free to post issues in the comments.
//increment post limit when checking out
function my_pmpro_after_checkout($user_id)
//might want to make this based on membership level
$old_value = get_user_meta($user_id, "user_post_limit", true);
update_user_meta($user_id, "user_post_limit", intval($old_value)+1, $old_value);
//make the user an author (ignore people who can already edit posts)
if(!user_can($user_id, "edit_posts"))
//upgrade the user to an author
$wp_user_object = new WP_User($user_id);
add_action("pmpro_after_checkout", "my_pmpro_after_checkout");
//Check limit before publishing.
function my_limit_num_posts($post_id)
global $wpdb;
$thepost = get_post($post_id);
$author_id = $thepost->post_author;
//ignore admins
if(!user_can($author_id, "manage_options"))
//how many posts have I published this month?
$sqlQuery = "SELECT COUNT(ID) FROM $wpdb->posts WHERE post_author = '" . $author_id . "' AND post_type = 'post' AND post_status = 'publish' AND post_date >= '" . date("Y-m-d 00:00:00") . "' ";
//use this query to get how many posts overall:
//$sqlQuery = "SELECT COUNT(ID) FROM $wpdb->posts WHERE post_author = '" . $author_id . "' AND post_type = 'post' AND post_status = 'publish' ";
$npublished = $wpdb->get_var($sqlQuery);
//what's my limit?
$user_post_limit = get_user_meta($author_id, "user_post_limit", true);
//am I over my limit?
if($npublished > $user_post_limit)
$upost = array();
$upost['ID'] = $post_id;
$upost['post_status'] = 'draft'; //force it back to draft
//show an error message
add_action('publish_post', 'my_limit_num_posts');
These functions pass a message # through the URL after saving to show the user why the post wasn't published.
function my_message_limit($loc)
//message=6 is the published message
if(strpos($loc, "&message=6"))
return add_query_arg(array('message'=>'1', 'mymsg'=>'1'), $loc);
return add_query_arg('mymsg', '1', $loc);
add_action('admin_notices', 'my_error_message');
function my_error_message()
if(!empty($_REQUEST['mymsg']) && $_REQUEST['mymsg'] == "1")
echo '<div class="error"><p>Your have reached your posts per month limit. <strong>This post was not published.</strong> Upgrading your account may increase your limit.</p></div>';

Hello thanks for the code, it works very well, but one question, how to build level pack with this? I mean level 1 for 5 posts, level 2 for 10 posts etc...

Thanks for great job i love your plugin


Yes I have the same need - any guidance appreciated.

Great plugin - thank you!

Hi there,

Where in this code is the number of allowed post per user being set?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment