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
. When I print it outside the variable, it returns a NULL value.
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.
To get this code to work properly, I have two ways, whichever works better.
my_logged_in_redirect()
inside the check_entry_countt
function so that it accesses the $count
variable.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). 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.
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:
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. Currently, the global variable $count is being identified as undefined outside.
Clarification - When I say the redirect function also stops working I mean this:
The redirect function is working outside the 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 }
Normally from the my_logged_in_redirect()
function; 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.