我在Wordpress中使用Peter的登录重定向将每个用户/角色重定向到其"个人页面"。
这一切都很好,但我想创建一个链接,任何用户都可以点击并重定向回其登录重定向页面。
现在,当用户登录时,它会转到他们的"个人页面",但当点击另一个链接时,就无法返回。
您应该能够使用作为插件一部分的redirect_to_front_page($redirect_to, $requested_redirect_to, $user)
。它主要用作login_redirect
过滤器的一部分,因此您可以在那里看到有关参数的更多信息。要在该过滤器之外使用,您可以执行以下操作,为重定向URLS传递false
,并检测它是否通过您的规则更新。
$user_id = get_current_user_id();
$user = get_user_by( 'id', $user_id );
if ( false !== ( $redirect_url = redirect_to_front_page( false, false, $user ) ) ){
// $redirect_url is the custom URL
} else {
// there is no custom URL
}
为了解决这个问题,我在functions.php中添加了一些代码,以便能够在小部件中运行php代码。
这是我添加到functions.php 中的代码
add_filter('widget_text','execute_php',100);
function execute_php($html){
if(strpos($html,"<"."?php")!==false){
ob_start();
eval("?".">".$html);
$html=ob_get_contents();
ob_end_clean();
}
return $html;
}
之后,我只添加HTML/PHP代码来创建一个链接,将当前用户重定向到其个人页面。我把这段代码放在了当用户登录我使用的登录小部件插件时应该显示的文本中。
<a href="<?php echo redirect_to_front_page( false, false, wp_get_current_user()); ?>">Your Page</a>
就这样。如果需要更多关于我所做的解释,请在下面留言。