WordPress用户登录登出后重定向到指定页面的实现方法
<span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>这篇文章将向您展示WordPress用户登录后如何重定向到指定页面或者文章的技巧。</span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><strong style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>一、重定向到网站管理面板。</strong><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'> </span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>将以下代码添加到您的当前主题的 functions.php 文件中:</span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><div style='margin: 8px 0px 1.1em; padding: 0px 0px 5px; outline: none; line-height: normal; border: 1px solid rgb(165, 165, 248); background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-family: "Courier New", monospace; overflow: hidden; width: 686px; word-break: break-all;'><div style="margin: 0px; padding: 5px 5px 5px 10px; outline: none; line-height: 21.6px; font-weight: bold; background: rgb(244, 245, 255); border-bottom: 1px solid rgb(242, 251, 246); height: 15px;">
<span style="line-height: 14px; font-size: 14px; float: left; padding-left: 5px;">PHP Code</span><span style="line-height: 12px; font-weight: normal; float: right; cursor: pointer;">复制内容到剪贴板</span>
</div>
<div id="code_7274" style="margin: 0px; padding: 0px; outline: none; line-height: 21.6px;">
<ol style="margin: 0px 1px 0px 45px; padding: 2px 0px; outline: none; list-style-position: initial; list-style-image: initial; line-height: 25px; word-break: break-word; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; border-left: 1px solid rgb(221, 221, 221); color: rgb(92, 92, 92);">
<li style="margin: 0px; padding: 0px 0px 0px 5px; outline: none; list-style: decimal; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; color: rgb(170, 0, 0);">
<span style="line-height: 14px; font-size: 14px; color: rgb(0, 0, 0);"><span style="line-height: 14px;"> </span><span style="line-height: 14px; color: blue; font-weight: bold;">function</span><span style="line-height: 14px;"> soi_login_redirect(</span><span style="line-height: 14px; color: rgb(221, 0, 0);">$redirect_to</span><span style="line-height: 14px;">, </span><span style="line-height: 14px; color: rgb(221, 0, 0);">$request</span><span style="line-height: 14px;">, </span><span style="line-height: 14px; color: rgb(221, 0, 0);">$user</span><span style="line-height: 14px;">) </span></span>
</li>
<li style="margin: 0px; padding: 0px 0px 0px 5px; outline: none; list-style: decimal; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; color: rgb(170, 0, 0);">
<span style="line-height: 14px; font-size: 14px; color: rgb(0, 0, 0);">{ </span>
</li>
<li style="margin: 0px; padding: 0px 0px 0px 5px; outline: none; list-style: decimal; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; color: rgb(170, 0, 0);">
<span style="line-height: 14px; font-size: 14px; color: rgb(0, 0, 0);"> </span><span style="line-height: 14px; font-size: 14px; color: blue; font-weight: bold;">return</span><span style="line-height: 14px; font-size: 14px; color: rgb(0, 0, 0);"> (</span><span style="line-height: 14px; font-size: 14px; color: rgb(0, 0, 0);">is_array</span><span style="line-height: 14px; font-size: 14px; color: rgb(0, 0, 0);">(</span><span style="line-height: 14px; font-size: 14px; color: rgb(221, 0, 0);">$user</span><span style="line-height: 14px; font-size: 14px; color: rgb(0, 0, 0);">->roles) && in_array(</span><span style="line-height: 14px; font-size: 14px; color: blue;">'administrator'</span><span style="line-height: 14px; font-size: 14px; color: rgb(0, 0, 0);">, </span><span style="line-height: 14px; font-size: 14px; color: rgb(221, 0, 0);">$user</span><span style="line-height: 14px; font-size: 14px; color: rgb(0, 0, 0);">->roles)) ? admin_url() : site_url(); </span>
</li>
<li style="margin: 0px; padding: 0px 0px 0px 5px; outline: none; list-style: decimal; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; color: rgb(170, 0, 0);">
<span style="line-height: 14px; font-size: 14px; color: rgb(0, 0, 0);">} </span>
</li>
<li style="margin: 0px; padding: 0px 0px 0px 5px; outline: none; list-style: decimal; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; color: rgb(170, 0, 0);">
<span style="line-height: 14px; font-size: 14px; color: rgb(0, 0, 0);">add_filter(</span><span style="line-height: 14px; font-size: 14px; color: blue;">'login_redirect'</span><span style="line-height: 14px; font-size: 14px; color: rgb(0, 0, 0);">, </span><span style="line-height: 14px; font-size: 14px; color: blue;">'soi_login_redirect'</span><span style="line-height: 14px; font-size: 14px; color: rgb(0, 0, 0);">, 10, 3); </span>
</li>
</ol>
</div>
</div>
<br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><strong style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>二、重定向到指定页面或者文章</strong><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'> </span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>您还可以将用户重定向到特定页面,代码如下:</span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><div style='margin: 8px 0px 1.1em; padding: 0px 0px 5px; outline: none; line-height: normal; border: 1px solid rgb(165, 165, 248); background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-family: "Courier New", monospace; overflow: hidden; width: 686px; word-break: break-all;'>
<div style="margin: 0px; padding: 5px 5px 5px 10px; outline: none; line-height: 21.6px; font-weight: bold; background: rgb(244, 245, 255); border-bottom: 1px solid rgb(242, 251, 246); height: 15px;">
<span style="line-height: 14px; font-size: 14px; float: left; padding-left: 5px;">PHP Code</span><span style="line-height: 12px; font-weight: normal; float: right; cursor: pointer;">复制内容到剪贴板</span>
</div>
<div id="code_3223" style="margin: 0px; padding: 0px; outline: none; line-height: 21.6px;">
<ol style="margin: 0px 1px 0px 45px; padding: 2px 0px; outline: none; list-style-position: initial; list-style-image: initial; line-height: 25px; word-break: break-word; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; border-left: 1px solid rgb(221, 221, 221); color: rgb(92, 92, 92);">
<li style="margin: 0px; padding: 0px 0px 0px 5px; outline: none; list-style: decimal; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; color: rgb(170, 0, 0);">
<span style="line-height: 14px; font-size: 14px; color: rgb(0, 0, 0);"><span style="line-height: 14px;"> </span><span style="line-height: 14px; color: blue; font-weight: bold;">function</span><span style="line-height: 14px;"> login_redirect( </span><span style="line-height: 14px; color: rgb(221, 0, 0);">$redirect_to</span><span style="line-height: 14px;">, </span><span style="line-height: 14px; color: rgb(221, 0, 0);">$request</span><span style="line-height: 14px;">, </span><span style="line-height: 14px; color: rgb(221, 0, 0);">$user</span><span style="line-height: 14px;"> ){ </span></span>
</li>
<li style="margin: 0px; padding: 0px 0px 0px 5px; outline: none; list-style: decimal; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; color: rgb(170, 0, 0);">
<span style="line-height: 14px; font-size: 14px; color: rgb(0, 0, 0);"> </span><span style="line-height: 14px; font-size: 14px; color: blue; font-weight: bold;">return</span><span style="line-height: 14px; font-size: 14px; color: rgb(0, 0, 0);"> home_url(</span><span style="line-height: 14px; font-size: 14px; color: blue;">'custom-page'</span><span style="line-height: 14px; font-size: 14px; color: rgb(0, 0, 0);">); </span>
</li>
<li style="margin: 0px; padding: 0px 0px 0px 5px; outline: none; list-style: decimal; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; color: rgb(170, 0, 0);">
<span style="line-height: 14px; font-size: 14px; color: rgb(0, 0, 0);">} </span>
</li>
<li style="margin: 0px; padding: 0px 0px 0px 5px; outline: none; list-style: decimal; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; color: rgb(170, 0, 0);">
<span style="line-height: 14px; font-size: 14px; color: rgb(0, 0, 0);">add_filter( </span><span style="line-height: 14px; font-size: 14px; color: blue;">'login_redirect'</span><span style="line-height: 14px; font-size: 14px; color: rgb(0, 0, 0);">, </span><span style="line-height: 14px; font-size: 14px; color: blue;">'login_redirect'</span><span style="line-height: 14px; font-size: 14px; color: rgb(0, 0, 0);">, 10, 3 ); </span>
</li>
</ol>
</div>
</div>
<span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>修改代码中的custom-page为某个页面或者文章的链接(注:不包括站点域名),如果删除custom-page会直接跳转到站点首页。 </span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><strong style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>三、自定义登出之后的重定向链接 </strong><br><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><div style='margin: 8px 0px 1.1em; padding: 0px 0px 5px; outline: none; line-height: normal; border: 1px solid rgb(165, 165, 248); background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-family: "Courier New", monospace; overflow: hidden; width: 686px; word-break: break-all;'>
<div style="margin: 0px; padding: 5px 5px 5px 10px; outline: none; line-height: 21.6px; font-weight: bold; background: rgb(244, 245, 255); border-bottom: 1px solid rgb(242, 251, 246); height: 15px;">
<span style="line-height: 14px; font-size: 14px; float: left; padding-left: 5px;">PHP Code</span><span style="line-height: 12px; font-weight: normal; float: right; cursor: pointer;">复制内容到剪贴板</span>
</div>
<div id="code_1440" style="margin: 0px; padding: 0px; outline: none; line-height: 21.6px;">
<ol style="margin: 0px 1px 0px 45px; padding: 2px 0px; outline: none; list-style-position: initial; list-style-image: initial; line-height: 25px; word-break: break-word; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; border-left: 1px solid rgb(221, 221, 221); color: rgb(92, 92, 92);">
<li style="margin: 0px; padding: 0px 0px 0px 5px; outline: none; list-style: decimal; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; color: rgb(170, 0, 0);">
<span style="line-height: 14px; font-size: 14px; color: rgb(0, 0, 0);"><span style="line-height: 14px;"> add_action(</span><span style="line-height: 14px; color: blue;">'wp_logout'</span><span style="line-height: 14px;">,</span><span style="line-height: 14px; color: blue;">'auto_redirect_after_logout'</span><span style="line-height: 14px;">); </span></span>
</li>
<li style="margin: 0px; padding: 0px 0px 0px 5px; outline: none; list-style: decimal; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; color: rgb(170, 0, 0);">
<span style="line-height: 14px; font-size: 14px; color: blue; font-weight: bold;">function</span><span style="line-height: 14px; font-size: 14px; color: rgb(0, 0, 0);"> auto_redirect_after_logout(){ </span>
</li>
<li style="margin: 0px; padding: 0px 0px 0px 5px; outline: none; list-style: decimal; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; color: rgb(170, 0, 0);">
<span style="line-height: 14px; font-size: 14px; color: rgb(0, 0, 0);"> wp_redirect( home_url() ); </span>
</li>
<li style="margin: 0px; padding: 0px 0px 0px 5px; outline: none; list-style: decimal; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; color: rgb(170, 0, 0);">
<span style="line-height: 14px; font-size: 14px; color: rgb(0, 0, 0);"> </span><span style="line-height: 14px; font-size: 14px; color: rgb(0, 0, 0);">exit</span><span style="line-height: 14px; font-size: 14px; color: rgb(0, 0, 0);">(); </span>
</li>
<li style="margin: 0px; padding: 0px 0px 0px 5px; outline: none; list-style: decimal; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; color: rgb(170, 0, 0);">
<span style="line-height: 14px; font-size: 14px; color: rgb(0, 0, 0);">} </span>
</li>
</ol>
</div>
</div>
<br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>以上即登出后跳转到网站首页</span>
頁:
[1]