网站优化

网站优化

Products

当前位置:首页 > 网站优化 >

WordPress:如何每次将特定用户重定向到特定页面? 这与登录后无关

GG网络技术分享 2025-03-18 16:12 42


问题描述:

I use WordPress.

I know how to redirect a user to a specific page after logging in. But let\'s say they have already logged-in and they click specific URL, they should be redirected to another.

Sample:

usernameA The user has already logged-in. He clicks domainname.com/my-account He will be redirected to domainname.com/pageA

usernameB The user has already logged-in. He clicks domainname.com/my-account He will be redirected to domainname.com/pageB

All of them click the domainname.com/my-account but they have to be redirected to a different page.

This redirection is per user. Is there a way to do this in function.php?

图片转代码服务由CSDN问答提供

感谢您的意见,我们尽快改进~

功能建议

我使用WordPress。</ p>

我知道如何将用户重定向到 登录后的特定页面。但是假设他们已经登录并且他们点击了特定的URL,他们应该被重定向到另一个。</ p>

示例:

usernameA用户已经登录 。 他点击domainname.com/my-account他将被重定向到domainname.com/pageA

usernameB用户已经登录。 他点击domainname.com/my-account他将被重定向到domainname.com/pageB

nn

他们中的所有人都会点击domainname.com/my-account但他们必须重定向到 不同的页面。</ p>

此重定向是每个用户。 有没有办法在function.php中执行此操作?</ p>

</ div>

网友观点:

You can use the built-in wp_redirect() function. Use a hook where there has been no output yet but wordpress already has the necessary info, like the requested post_id. you can stick something like this in your functions.php

function myredirect() {

$page_id = get_queried_object_id();

//im working with the id\'s here, but for better readability you can also work with slugs.

//you shouldnt use the whole url, because it will mean trouble when changing/moving domain-names

//

//check if it\'s the page you want to redirect from.

if($page_id==138) {

$user_id=get_current_user_id();

$redirect_to=\'\';

switch($user_id) {

case 1:

$redirect_to=54840;

break;

}

if(!empty($redirect_to)) {

wp_redirect(get_permalink($redirect_to));

exit;

}

}

}

add_action(\'template_redirect\', \'myredirect\');

标签:

提交需求或反馈

Demand feedback