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.

Question

Is there a way that my registered users (if they are not the form is hidden so I don't need that) are restricted to submit the form more than 5 times per day, or 20 per month or whatever value I would use?

And I would also need to display the number of sumbmissions they have left. If they already sent the form 3 times they would have the function echo something like: "You have 2 submissions left for today".

Form code (like every other form with submit on bottom :) )

<form id="submiter" method="post" action="<?php echo get_author_custom_permalink(   
b_extract_user_info('user_id') , 'submit' ); ?>" onsubmit="return ray.ajax()" 
enctype="multipart/form-data">

bla bla bla form fields...

<input type="submit" value="<?php print __('Submit', 'an'); ?>" class="button-form" />
<input type="hidden" name="user_id_value" value="<?php echo   
b_extract_user_info('user_id'); ?>" />
<input type="hidden" name="action" value="wp_handle_upload" />
<input type="hidden" name="jj_form_action" value="submit_post" />

i would really appreciate any help! Thanks everyone :)

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted
+50

As @amit wrote, the submission should have a daily counter that saves the count for that user, in the wp_usermeta table.

If you can run a cron job that runs daily, you can save the submission counter only. The daily cron can reset the counter on assigned time. But if you don't you should save the counter and the day also.

$current_user = wp_get_current_user();

$old_count = get_user_meta($current_user->ID,'form-counter', true);

if($old_count['day'] == $today){ // still the same day
    $new_count['day'] = $today;
    $new_count['count'] = $old_count;
} else { // the day after the last submission 
    $new_count['day'] = $today;
    $new_count['count'] = 1;        
}

update_user_meta( $current_user->ID, 'form-counter', $new_count, $old_count);

The form itself can have a simple conditional that checks current user's meta value.

$current_user = wp_get_current_user();
$counter = get_user_meta($current_user->ID,'form-counter', true);
if($counter['day'] != $today || ($counter['day'] == $today && $counter['count'] < 5)){
    get_template_part('form');
} else {
    echo ' You have reached your daily limit';
}

Hope this help

Update

I'm not sure how your form handle ajax using the get_author_custom_permalink().

Here is the code that goes to your functions.php, if you are using wp_ajax to handle form submission.

Please note that this code is untested.

add_action('wp_ajax_my_custom_form', 'process_my_custom_form');

function process_my_custom_form() {
    global $current_user;
    // validate nonce
    if ( empty($_POST) || !wp_verify_nonce($_POST[$current_user->user_login],'form_process') ) {
        echo 'You targeted the right function, but sorry, your nonce did not verify.';
        die();
    } else {
        // validate post data
        $input_1 = $_POST['input_1'];
        $input_2 = $_POST['input_2'];


    // process data


    // start counter here
    $old_count = get_user_meta($current_user->ID,'form-counter', true);
    $today = date('YY-MM-DD');
            // check if the user havent sent a submission at all
            if(!$old_count){
        $new_count['day'] = $today;
        $new_count['count'] = 0;
                }

    if($old_count['day'] == $today){ // still the same day
        $new_count['day'] = $today;
        $new_count['count'] = $old_count;

    } else { // the day after the last submission 
        $new_count['day'] = $today;
        $new_count['count'] = 1;        
    }
    update_user_meta( $current_user->ID, 'form-counter', $new_count, $old_count);
    $submission_limit = 5
    $message = 'You have '.$submission_limit - $new_count['count'].' submission left';
    echo $message;
    die();
    }
}

And on your form

$counter = get_user_meta($current_user->ID,'form-counter', true);
$submission_limit = 5

if($counter['day'] != $today || ($counter['day'] == $today && $counter['count'] < $submission_limit)){

    echo 'You have '.$submission_limit - $counter['count'].' submission left';

    get_template_part('form');
} else {
    echo ' You have reached your daily limit';
}
share|improve this answer
    
Hey, great, but how and where do I implement this code? I'm not really that good:) In the form, outside the form in functions.php in header?! thanks so much man! –  jimilesku Aug 12 '12 at 19:39
    
Do I place my form in here: get_template_part('form'); and how? how do I do it based upon my upper form php? I would love to learn: –  jimilesku Aug 12 '12 at 20:56
    
The code goes to your functions.php, I'll update my answer –  ifdion Aug 12 '12 at 21:32
    
I actually dont use ajax, it is just a loading bar, it has no effect on the form. Is the code still the same?! –  jimilesku Aug 12 '12 at 22:04
    
I'm not sure how you use get_author_custom_permalink(). What does it prints out? If you are using the wp_ajax, the function's action attribute should be action="<?php echo admin_url('admin-ajax.php'); ?>" –  ifdion Aug 12 '12 at 22:12
show 5 more comments

I don't know which forms you are using but the basic form submission loads the data into database.

To be able to track submission you need to store the data in database. In this case if you able to modify that form to store additional data such as date, user id, submissions etc. then you can further easily do mysql queries and determine how many submissions are remaining etc.

share|improve this answer
    
I'm using a normal form, no plugin. Do You have an example of how this would work? –  jimilesku Aug 6 '12 at 8:43
add comment

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.