0

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.com/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.com/private-page/'.$slug.'/');
                 exit;
        }
    }

Nothing Happens when I go to the menu page besides it showing the original content for the page.

0

The first problem I see is here:

if( get_page_by_slug($slug) ){
}wp_redirect('https://example.com/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.com/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 be functions.php (note the s) in the currently-active theme.
  • Where are these functions being called from? What action/filter hook are you adding them to?
  • 1
    I actually had already fixed that if() statement problem and it didn't fix the problem. It was a mistype on myself to put "function.php" I actually did it on functions.php. – Jacob Rowe Aug 11 at 19:38
  • I am not sure what you are referring to when you ask where are the functions being called from and what action/filter hook I am adding to them. I am new to php so sorry if I am unfamiliar with some of the objects/controls that are used in the code. Thanks for the help. – Jacob Rowe Aug 11 at 19:40
  • Just introducing functions to functions.php won't result in your code being run. You'll need to hook them into WordPress somewhere, almost always by using an action hook or a filter hook. – Pat J Aug 11 at 19:46
  • Which hook should I use and do I just add it in the same code? – Jacob Rowe Aug 11 at 20:01
  • 1
    I figured it out a few hours ago by using "template_redirect" thanks for the help. – Jacob Rowe Aug 12 at 0:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.