简体   繁体   English

如何根据 WordPress 中的表单条目数将用户重定向到不同的页面

[英]How to redirect a user to a different page based on the number of form entries in WordPress

I am trying to redirect a user based on whether or not they have an entry in a form.我正在尝试根据用户是否在表单中有条目来重定向用户。 I am using formidable plugin.我正在使用强大的插件。 I set the variable $count to be a global variable but for some reason I can't get it to work outside of the function check_entry_countt .我将变量 $count 设置为全局变量,但由于某种原因,我无法让它在函数check_entry_countt之外工作。 When I print it outside the variable, it returns a NULL value.当我在变量外部打印它时,它返回一个 NULL 值。

To avoid this, I decided to use the my_logged_in_redirect() function inside the check_entry_countt function but then the redirect function also stops working.为了避免这种情况,我决定在check_entry_countt函数中使用my_logged_in_redirect()函数,但随后重定向函数也停止工作。

To get this code to work properly, I have two ways, whichever works better.为了让这段代码正常工作,我有两种方法,哪个效果更好。

  1. To use the redirect function my_logged_in_redirect() inside the check_entry_countt function so that it accesses the $count variable.check_entry_countt函数中使用重定向函数my_logged_in_redirect()以便访问$count变量。
  2. To use the redirect function my_logged_in_redirect() outside the check_entry_countt function but then get the function to access the $count variable as a global variable (which is not working for me).check_entry_countt函数之外使用重定向函数my_logged_in_redirect() ,然后让函数将 $count 变量作为全局变量访问(这对我不起作用)。 The code.编码。
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
    }
  }

}

The redirect function重定向功能

  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' );

The goal: So that elseif ($count == 0) { //i can run the redirect function} as shown in the code.目标:这样elseif ($count == 0) { //i can run the redirect function}如代码所示。

If I was able to fetch the value of the global variable $count, inside my redirect function, I'd simply add the variable inside the if statement as so:如果我能够在重定向函数中获取全局变量 $count 的值,我只需将变量添加到 if 语句中,如下所示:

if ( ($count == 0 && is_front_page() || is_page('profile') || is_page('account')) && is_user_logged_in()){...} Whichever options works, either fetching the global variable outside check_entry_countt function or making my redirect function my_logged_in_redirect() work inside the check_entry_countt function, I'm fine. 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函数中工作,我很好。 Currently, the global variable $count is being identified as undefined outside.目前,全局变量 $count 在外部被标识为未定义。

Clarification - When I say the redirect function also stops working I mean this:澄清 - 当我说重定向功能也停止工作时,我的意思是:

The redirect function is working outside the check_entry_countt function .重定向函数在check_entry_countt function之外工作。 No redirection happens when I insert the redirect function inside this if statement elseif ($count == 0) { // the redirect function stops redirecting when added here }当我在这个 if 语句中插入重定向函数时没有重定向发生elseif ($count == 0) { // the redirect function stops redirecting when added here }

Normally from the my_logged_in_redirect() function;通常来自my_logged_in_redirect()函数; Use must work properly Consider my example:使用必须正常工作考虑我的例子:

The first example:第一个例子:

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();
            
        }
    }

}

The redirect function重定向功能

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;
    }
}

Remove this hook :删除这个钩子:

add_action( 'template_redirect', 'my_logged_in_redirect' );

The second example If this does not work, set the value with the parameter :第二个示例如果这不起作用,请使用参数设置值:

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');

For the second example, because the condition is inside the function, we do not need to check here对于第二个例子,因为条件在函数内部,我们这里不需要检查

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM