![](/img/trans.png)
[英]How to redirect a button to a different page based on used login on Wordpress
[英]How to redirect a user to a different page based on the number of form entries in WordPress
我正在嘗試根據用戶是否在表單中有條目來重定向用戶。 我正在使用強大的插件。 我將變量 $count 設置為全局變量,但由於某種原因,我無法讓它在函數check_entry_countt
之外工作。 當我在變量外部打印它時,它返回一個 NULL 值。
為了避免這種情況,我決定在check_entry_countt
函數中使用my_logged_in_redirect()
函數,但隨后重定向函數也停止工作。
為了讓這段代碼正常工作,我有兩種方法,哪個效果更好。
check_entry_countt
函數中使用重定向函數my_logged_in_redirect()
以便訪問$count
變量。check_entry_countt
函數之外使用重定向函數my_logged_in_redirect()
,然后讓函數將 $count 變量作為全局變量訪問(這對我不起作用)。 編碼。add_action( 'wp_enqueue_scripts', 'child_enqueue_styles', 15 );
nocache_headers();
add_action('frm_display_form_action', 'check_entry_countt', 8, 3);
function check_entry_countt($params, $fields, $form){
global $user_ID;
$current_user = wp_get_current_user();
remove_filter('frm_continue_to_new', '__return_false', 50);
if( $form->id == 4 && !is_admin() && user_can( $current_user, "booknetic_staff" ) ){ //replace 4 or 5 with the ID of your form
global $count;
$count = FrmEntry::getRecordCount("form_id=". $form->id ." AND user_id=".$user_ID);
if($count >= 1){ //change 1 to your entry limit
echo 'You have already submitted your data. Please visit your profile/account if you want to update';
add_filter('frm_continue_to_new', '__return_false', 50);
} elseif ($count == 0) {
//i need to run the redirect function here
}
}
}
重定向功能
function my_logged_in_redirect() {
if ( (is_front_page() || is_page('profile') || is_page('account')) && is_user_logged_in())
{
wp_redirect( '/first-time-application-temp/');
die;
}
}
add_action( 'template_redirect', 'my_logged_in_redirect' );
目標:這樣elseif ($count == 0) { //i can run the redirect function}
如代碼所示。
如果我能夠在重定向函數中獲取全局變量 $count 的值,我只需將變量添加到 if 語句中,如下所示:
if ( ($count == 0 && is_front_page() || is_page('profile') || is_page('account')) && is_user_logged_in()){...}
無論哪個選項有效,要么在check_entry_countt
之外獲取全局變量函數或使我的重定向函數my_logged_in_redirect()
在check_entry_countt
函數中工作,我很好。 目前,全局變量 $count 在外部被標識為未定義。
澄清 - 當我說重定向功能也停止工作時,我的意思是:
重定向函數在check_entry_countt function
之外工作。 當我在這個 if 語句中插入重定向函數時沒有重定向發生elseif ($count == 0) { // the redirect function stops redirecting when added here }
通常來自my_logged_in_redirect()
函數; 使用必須正常工作考慮我的例子:
第一個例子:
add_action('frm_display_form_action', 'check_entry_countt', 8, 3);
function check_entry_countt($params, $fields, $form)
{
global $user_ID;
$current_user = wp_get_current_user();
remove_filter('frm_continue_to_new', '__return_false', 50);
if ($form->id == 4 && !is_admin() && user_can($current_user, "booknetic_staff")) { //replace 4 or 5 with the ID of your form
$count = FrmEntry::getRecordCount("form_id=" . $form->id . " AND user_id=" . $user_ID);
if ($count >= 1) { //change 1 to your entry limit
echo 'You have already submitted your data. Please visit your profile/account if you want to update';
add_filter('frm_continue_to_new', '__return_false', 50);
} elseif ($count == 0) {
my_logged_in_redirect();
}
}
}
重定向功能
function my_logged_in_redirect() {
if ( (is_front_page() || is_page('profile') || is_page('account')) && is_user_logged_in())
{
wp_redirect( '/first-time-application-temp/');
die;
}
}
刪除這個鈎子:
add_action( 'template_redirect', 'my_logged_in_redirect' );
第二個示例如果這不起作用,請使用參數設置值:
add_action('frm_display_form_action', 'check_entry_countt', 8, 3);
function check_entry_countt($params, $fields, $form)
{
global $user_ID;
$current_user = wp_get_current_user();
remove_filter('frm_continue_to_new', '__return_false', 50);
if ($form->id == 4 && !is_admin() && user_can($current_user, "booknetic_staff")) { //replace 4 or 5 with the ID of your form
$count = FrmEntry::getRecordCount("form_id=" . $form->id . " AND user_id=" . $user_ID);
if ($count >= 1) { //change 1 to your entry limit
echo 'You have already submitted your data. Please visit your profile/account if you want to update';
add_filter('frm_continue_to_new', '__return_false', 50);
}
my_logged_in_redirect($count);
}
}
function my_logged_in_redirect($count)
{
if (!empty($count) && $count == 0 && ((is_front_page() || is_page('profile') || is_page('account')) && is_user_logged_in())) {
wp_redirect('/first-time-application-temp/');
die;
}
}
add_action('template_redirect', 'my_logged_in_redirect');
對於第二個例子,因為條件在函數內部,我們這里不需要檢查
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.