0

I currently use wpadverts plugin and wants to limit the submission to 10 submit / month.

How can I do that?

I think I need to make a new row like "form_submissions" in "wp_usermeta" and store every time when the user submitted the form and make a cron task to delete the row value after a month.

And I need to query the row value on the form submit page like this:

$result = mysqli_query($connect, "SELECT COUNT(*) AS `form_submission` FROM wp_usermeta WHERE user_id = $user_id");

    $row = mysqli_fetch_assoc($result);

    if($row['form_submission'] > 10) {
        // can't submit the form because you already have 10 post
    } else {
        // you can submit the form
    }

Or I'm totally lost? Sorry for my bad english.

4
  • with WordPress you can use update_user_meta and get_user_meta to make it easier on yourself instead of writing queries every time.
    – majick
    Commented Jun 13, 2019 at 1:20
  • Thanks for your reply. And how can I add +1 to the current value if I'm using update_user_meta? So I wants to add +1 every time when user submit new post I tried update_user_meta( $user_id, 'form_submission', 'form_submission' +1, true ); but doesn't work. It will always be 1
    – gezuka
    Commented Jun 13, 2019 at 2:38
  • $value = get_user_meta($user_id, 'form_submission', true); if (!$value) {$value = 1;} else {$value = $value + 1);} update_user_meta($user_id, 'form_submission', $value);
    – majick
    Commented Jun 13, 2019 at 6:47
  • Okay, I did it, thanks for your help. Can you write an answer so I can accept?
    – gezuka
    Commented Jun 13, 2019 at 10:17

1 Answer 1

0

You can do it using get_user_meta and update_user_meta:

$value = get_user_meta($user_id, 'form_submission', true); 
if (!$value) {$value = 1;} else {$value = $value + 1;}
if ($value < 11) {
    update_user_meta($user_id, 'form_submission', $value);
} else {
    // too many form submissions
}

You could also expand on this to store the submission month too if you don't want to use a cron job.

$month = get_user_meta($user_id, 'submission_month', true);
$value = get_user_meta($user_id, 'form_submission', true); 

if ($month && ($month != date('m',time())) ) {
    update_user_meta($user_id, 'submission_month', date('m', time());
    $newmonth = true;
} else {$newmonth = false;}
if (!$value || $newmonth) {$value = 1;} else {$value = $value + 1;}
if ($value < 11) {
    update_user_meta($user_id, 'form_submission', $value);
} else {
    // too many form submissions
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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