0
votes

I'm trying to make the front-end login for Wordpress redirect to the post (in a custom post type) that was automatically created when they registered.

I can get the URL I want to redirect them with a wp_query. I imagine this is not the best way to do it, but I don't know enough php to figure it out. Here's my current attempt, but it just prints the url (the right one, at least!) on a blank page with the same login url they were already at:

function my_login_redirect( $redirect_to, $request, $user ){
    global $user, $post;
    $args = array(
       'author' => $current_user->ID,
       'post_type' => 'course-providers',
       'showposts' => 1,
       'caller_get_posts' => 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);

    if( $my_query->have_posts() ) {
    while ($my_query->have_posts()) : $my_query->the_post(); ?>
       <?php wp_redirect ( the_permalink () ); ?>
       <?php 
    endwhile;
    } else {
        echo "This User Has no Profile";
    }

}
add_filter("login_redirect", "my_login_redirect", 10, 3);

Also, I imagine I don't need the wp_redirect and that I should just be using the login_redirect filter itself, but again, I'm pretty lost right now and just taking lots of shots in the dark.

Thanks for the help, and let me know if there's additional info that would make this more helpful for others or easier to answer. Thanks!

1
have you tried a simple header("Location: /correctURL"); redirect?Kevin Lynch
No... I just tried changing the <?php wp_redirect line to: <?php header("Location:" . the_permalink () ); ?> and it output the same thing as the wp_redirect line. Thanks for the idea though! Or - if that wasn't what you had in mind, let me know :)Jerry

1 Answers

0
votes

I ended up using a template redirect to make this work. I assume there's technically a better way to do it, but it's loading extremely fast and doing exactly what I need it to.

So, now, when a user logs in it goes to a direct url- /profiles - and the template on that page is just a redirect. I used the ideas and some sample code from this smashing magazine post on random redirects to make it work.

Here's the function I used in my functions.php file for the template to make the redirect happen:

function profile_redirect() {
// This is a template redirect
// Whenever someone goes to /profile (or any page using the profile template)
// this function gets run

if ( is_user_logged_in() ) {
    global $current_user, $post;
    $args = array(
        'author' => $current_user->ID,
        'post_type' => 'profile',
        'posts_per_page' => 1
        );
    $my_query = null;
    $my_query = new WP_Query($args);

    if( $my_query->have_posts() ) {
        while ( $my_query->have_posts() )
            $my_query->the_post();
            //We have a post! Send them to their profile post.
            wp_redirect ( get_permalink () );
        exit;
    } else {
        // If there are no posts, send them to the homepage
        wp_redirect ( get_bloginfo('url') );
        exit;
    }
    wp_reset_query();
} else {
    // If they're not logged in, send them to the homepage
    wp_redirect ( get_bloginfo('url') );
    exit;
}

}

Then, on my profile template, I put this at the top with the opening php tag to run the function:

profile_redirect(); ?>

This is working for me, so I'll leave it as is for now :)