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?
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745250846a4618668.html
评论列表(0条)