2017-01-09 145 views
0

我目前已將以下代碼添加到我的functions.php中,以便在用戶訪問我的網站時重定向用戶。如果'update_racer_record'的值被設置爲'YES',用戶只會被重定向。Wordpress重定向問題

但是,我收到了'太多重定向'的問題,因爲即使他到達最後一頁,Wordpress仍然會重定向用戶。爲了阻止這種情況,我添加了'is_page'條件,但這似乎不起作用。我仍然收到'重定向太多'的錯誤。

我錯過了什麼?

add_action('init', 'my_redirect'); 

function my_redirect(){ 

    global $wpdb; 

    // if user is logged in 
    if(is_user_logged_in()) { 

     // set vars 
     $racer_id = find_racer_id(); 
     $update_racer_record = 'NO'; 

     // check to see if the user record has been updated in the last 6 months 
     $update_racer_record = $wpdb->get_var(" 
       select 'YES' 
        from flx_racers 
       where racer_id = " . $racer_id . " 
        and ifnull(update_date, date_sub(now(), interval 7 month)) < date_sub(now(), interval 6 month); 
     "); 

     // if user record has not been updated in the last 6 months, redirect 
     if ($update_racer_record == 'YES') { 

     // if we are not currently already in the personal info page, then redirect to it 
     if (!is_page('flx-racer-personal-info')) { 
      wp_redirect(site_url().'/flx-racer-personal-info/'); exit; 
     } 

     } 

    } 

} 

回答

0

我修好了,現在它完美的工作!通過簡單地改變ADD_ACTION代碼...

從這:

add_action('init', 'my_redirect'); 

要這樣:

add_action('template_redirect', 'my_redirect');