Wordpress重定向问题

时间:2017-01-09 18:41:10

标签: wordpress

我目前将以下代码添加到我的functions.php中,以便在用户访问我的网站时重定向。只有在' update_racer_record'的值时才会重定向用户。设置为“是”'

然而,我收到了太多的重定向'问题是因为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;
        }

      }

    }

}

1 个答案:

答案 0 :(得分:0)

我修好了,现在它完美无缺!只需更改add_action代码...

由此:

add_action( 'init', 'my_redirect' );

对此:

add_action( 'template_redirect', 'my_redirect' );