正如@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\';
}