如何让注册用户每天只提交5次表格?

时间:2012-08-05 作者:jimilesku

问题

是否有办法限制我的注册用户(如果他们不是,那么表单是隐藏的,所以我不需要)每天提交表单5次以上,或者每月提交20次以上,或者我使用的任何值?

我还需要显示他们剩下的sumbmissions数量。如果他们已经发送了三次表单,那么他们会使用类似“echo”这样的函数:;您今天还有2份提交;。

表单代码(与其他底部有submit的表单一样:))

<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" />
我真的很感激任何帮助!谢谢大家:)

2 个回复
最合适的回答,由SO网友:ifdion 整理而成

正如@amit所写的,提交应该有一个每日计数器,将该用户的计数保存在wp\\u usermeta表中。

如果可以运行每天运行的cron作业,则只能保存提交计数器。每日cron可以在指定的时间重置计数器。但如果你不这样做,你应该节省柜台和时间。

$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);
表单本身可以有一个简单的条件来检查当前用户的元值。

$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\';
}
希望这有帮助

更新我不确定表单如何使用get_author_custom_permalink().

下面是指向函数的代码。php,如果您使用wp\\U ajax处理表单提交。

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();
    }
}
在你的表格上

$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\';
}

SO网友:amit

我不知道您正在使用哪些表单,但基本表单提交会将数据加载到数据库中。

为了能够跟踪提交,您需要将数据存储在数据库中。在这种情况下,如果您能够修改该表单以存储其他数据,如日期、用户id、提交等,那么您可以进一步轻松地进行mysql查询,并确定剩余提交的数量等。

结束

相关推荐

使用wp_Dropdown_USERS和SELECTED()函数?

我有一个自定义的元框,其中有一个下拉列表,通过WP\\u dropdown\\u users显示所有WP用户。它保存了使用post选择的WP用户,以便所有工作正常。但是,是否有办法使其在返回编辑时在下拉列表中显示选定的用户,而不是列表中的第一个用户?我已经将selected()函数用于另一个下拉列表,但我已经动态创建了它。我尝试用wp\\u dropdown\\u用户实现selected(),但没有成功,所以我不确定这是否可行?谢谢