Skip to content

Instantly share code, notes, and snippets.

@kimcoleman
Last active November 12, 2024 17:36
Show Gist options
  • Save kimcoleman/7040d7c885084539885056a8bf172da9 to your computer and use it in GitHub Desktop.
Save kimcoleman/7040d7c885084539885056a8bf172da9 to your computer and use it in GitHub Desktop.
Set the submission limit of a WS Form based on the logged-in member's Membership Level.
<?php
/**
* Check how many posts a member has submitted to a specific form on page ID '48'.
* Hide the form if they have reached the submission limit for their Membership Level.
*/
function my_pmpro_submission_limit_hide_ws_form( $content ) {
// Return early if PMPro is not active or the user is not logged in.
if ( ! function_exists( 'pmpro_has_membership_access' ) || ! is_user_logged_in() ) {
return $content;
}
// Return if the user does not have access. Let PMPro handle the protection.
if ( ! pmpro_has_membership_access() ) {
return $content;
}
// Are we on the page for submitting news?
if ( ! is_page( '48' ) ) {
return $content;
}
// Ok, we are on the page and we have the shortcode. Get current user ID
$user_id = get_current_user_id();
// Get the current month and year
$current_month = date( 'm' );
$current_year = date( 'Y' );
// Query for the number of posts the user has authored this month
global $wpdb;
$post_count = $wpdb->get_var(
$wpdb->prepare(
"SELECT COUNT(*) FROM $wpdb->posts
WHERE post_author = %d
AND post_type = 'post'
AND MONTH(post_date) = %d
AND YEAR(post_date) = %d",
$user_id,
$current_month,
$current_year
)
);
// Check membership level and set submission limit accordingly
$submission_limit = 1; // Default limit for membership levels
if ( pmpro_hasMembershipLevel( 2 ) ) {
$submission_limit = 5; // Custom limit for level 2
}
// Hide form and replace shortcode if the user's post count exceeds the submission limit
if ( $post_count >= $submission_limit ) {
// Custom message to display instead of the form
$custom_message = __('You have reached your submission limit for this month. Please contact support if you need assistance.', 'textdomain');
// Replace the [ws_form ...] shortcode with the custom message
$content = preg_replace( '/(\[ws_form[^\]]*\]|<!-- wp:wsf-block\/form-add[^\>]*\/-->)/', $custom_message, $content );
}
// Return the updated content.
return $content;
}
add_filter( 'the_content', 'my_pmpro_submission_limit_hide_ws_form', 5, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment