我將下面的代碼扔進了wordpress模板頁面,它工作正常,但是如何在頁面加載而不是點擊時執行此鏈接(帶有加載的PHP參數)?在wordpress模板中重定向之前執行php
<?php
$current_user = wp_get_current_user();
echo ('<a href=https://portal.pension-resources.com/sso?u='.
$current_user->user_email .'>PRI LINK</a>');
?>
我將下面的代碼扔進了wordpress模板頁面,它工作正常,但是如何在頁面加載而不是點擊時執行此鏈接(帶有加載的PHP參數)?在wordpress模板中重定向之前執行php
<?php
$current_user = wp_get_current_user();
echo ('<a href=https://portal.pension-resources.com/sso?u='.
$current_user->user_email .'>PRI LINK</a>');
?>
<?php
if (!isset($_GET["u"])) {
$current_user = wp_get_current_user();
//check to see if u is the same as current user's email
if ($current_user->user_email !== $_GET['u']) {
wp_redirect("https://portal.pension-resources.com/sso?u=".$current_user->user_email);
exit;
}
}
理想情況下,你將需要包裝內的這頭重定向功能,如果條件檢查,以防止無限循環。
這是訣竅,你是我的英雄。 –
wp_redirect($url);
exit;
原始php:'header('Location:'。$ current_user-> user_email。'')' – taha
@GrumpyCrouton有點不同,因爲OP要求在wordpress中完成它,並將當前wp用戶添加爲GET參數。 – williamli
@williamli我們不把注意力放在單個情景上,而是作爲整個新問題。我鏈接的帖子解釋瞭如何在PHP中進行重定向,添加一個GET變量非常簡單,並且可以從其他帖子回答,例如[PHP重定向到帶有URL變量的頁面](https ://stackoverflow.com/questions/14007750/php-redirect-to-page-with-variable-in-url)。個別情景通常對未來的讀者不是有幫助的,因此這個問題將會是無關緊要的,因爲它已經得到了回答。 – GrumpyCrouton