admin管理员组文章数量:1418065
I am setting up a new website where I am managing a small amount of clients. I have created a menu page that when the user is logged out it basically just tells the user to login. But I want to code for when a user is logged in and clicks on that same menu page, I want it to redirect to a private page I have created for them. Each individual already has a private page. The URL for each private page is "example/private-page/username/. If the URL doesn't exist for an individual I just want the code to go back to the original menu page not a nonexistent page.
I have tried different ways including using meta to refresh the page to the other url. I can't figure what exactly isn't working. I am typing the code on the function.php
function get_page_by_slug( $slug ) {
if( $pages = get_pages() )
foreach( $pages as $page )
if( $slug === $page->post_name ) return true;
return false;
}
function userredirect() {
$current_user = wp_get_current_user();
$slug = $current_user->user_login;
if( is_user_logged_in() && is_page('Menu Page') ){
if( get_page_by_slug($slug) ){
}wp_redirect('/'.$slug.'/');
exit;
}
}
Nothing Happens when I go to the menu page besides it showing the original content for the page.
I am setting up a new website where I am managing a small amount of clients. I have created a menu page that when the user is logged out it basically just tells the user to login. But I want to code for when a user is logged in and clicks on that same menu page, I want it to redirect to a private page I have created for them. Each individual already has a private page. The URL for each private page is "example/private-page/username/. If the URL doesn't exist for an individual I just want the code to go back to the original menu page not a nonexistent page.
I have tried different ways including using meta to refresh the page to the other url. I can't figure what exactly isn't working. I am typing the code on the function.php
function get_page_by_slug( $slug ) {
if( $pages = get_pages() )
foreach( $pages as $page )
if( $slug === $page->post_name ) return true;
return false;
}
function userredirect() {
$current_user = wp_get_current_user();
$slug = $current_user->user_login;
if( is_user_logged_in() && is_page('Menu Page') ){
if( get_page_by_slug($slug) ){
}wp_redirect('https://example/private-page/'.$slug.'/');
exit;
}
}
Nothing Happens when I go to the menu page besides it showing the original content for the page.
Share Improve this question asked Aug 11, 2019 at 19:06 Jacob RoweJacob Rowe 31 bronze badge1 Answer
Reset to default 0The first problem I see is here:
if( get_page_by_slug($slug) ){
}wp_redirect('https://example/private-page/'.$slug.'/');
exit;
The if()
statement will do nothing; your wp_redirect()
and exit
statements are outside the {}
braces.
Try this instead:
if( get_page_by_slug($slug) ){
wp_redirect('https://example/private-page/'.$slug.'/');
exit;
}
Aside from that, a couple other things to check:
- You say the code is in
function.php
; if you're referring to a theme file, it needs to befunctions.php
(note thes
) in the currently-active theme. - Where are these functions being called from? What action/filter hook are you adding them to?
本文标签: phpRedirect User when they click Menu Option
版权声明:本文标题:php - Redirect User when they click Menu Option 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745250846a2649813.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论