2015-11-03 62 views
-1

我正在嘗試創建一個非常基本的插件,用於檢測用戶是否登錄的Wordpress頁面上的重定向。我能得到的只是if語句的工作,但我加入else語句,不知道什麼語法我有錯,創建一個致命的錯誤的一部分:構造Wordpress PHP重定向插件

<?php 
/* 
Plugin Name: Three Dot Redirects 
*/ 

add_action('wp_head', 'rhombus_gate_redirect'); 
function rhombus_gate_redirect() { 
    if (is_page('rhombus_gate') && is_user_logged_in()) { 
    wp_redirect (home_url("/constructs/pizza")); 
    } 
    else (is_page('rhombus_gate')) { 
    wp_redirect (home_url("/gate")); 
    } 

} 
+0

到底是什麼錯誤? – rnevius

回答

1

wp_head行動掛鉤太由於標題已經發送,所以遲到重定向。

相反,我建議使用template_redirect hook,它會在WordPress加載完成之後但在發送任何標題之前觸發。用戶在此階段被認證,並確定頁面模板。

else也並不需要一個條件......並wp_redirect()應該始終跟着exit

add_action('template_redirect', 'rhombus_gate_redirect'); 
function rhombus_gate_redirect() { 
    if (is_page('rhombus_gate')) { 
     $redirect_url = is_user_logged_in() ? home_url("/constructs/pizza") : home_url("/gate"); 
     wp_redirect ($redirect_url); 
     exit; 
    } 
}