NOTE
- You should add
exit()
or die()
after wp_redirect()
;
- Use
wp
instead on init
. This will ensure you the template is already loaded.
- If the template file is under subdirectory then you have to check with that part. Ex:
/wp-content/themes/my_active_theme/page-templates/list-projects.php
, then you have to check page-templates/list-projects.php
這裏是代碼,會爲你工作:
function wh_redirect_sample()
{
if (basename(get_page_template()) == 'list-projects.php')
{
wp_redirect('http://url.com.au/profile');
exit(); //always remember to add this after wp_redirect()
}
}
add_action('wp', 'wh_redirect_sample');
備選方法:
function wh_redirect_sample()
{
//if list-projects.php is under sub directory say /wp-content/themes/my_active_theme/page-templates/list-projects.php
if (is_page_template('page-templates/list-projects.php'))
{
wp_redirect('http://url.com.au/profile');
exit();
}
//if list-projects.php is under active theme directory say /wp-content/themes/my_active_theme/list-projects.php
if (is_page_template('list-projects.php'))
{
wp_redirect('http://url.com.au/profile');
exit();
}
}
add_action('wp', 'wh_redirect_sample');
代碼發送到您活動的兒童主題(或主題)的function.php文件中。或者也可以在任何插件php文件中使用。
代碼已經過測試和工作。
希望這會有所幫助!
感謝您指出,雖然我仍然有訪問頁面時獲取函數觸發的初始問題。 – Elevant
+1爲你的答案,你錯過了一個重要的點是'init',它是在模板加載之前調用的。 –