wpf_return_after_login_url

#Overview

This filter is run when redirecting a user back to a piece of restricted content using the Return After Login feature.

It runs after WP Fusion has already confirmed they now have access to the restricted content (to redirect the user if access is still denied, use the wpf_redirect_url filter).

To use the code examples below, add them to your active theme’s functions.php file.

#Parameters

  • $url: The URL that the user will be redirected to (typically the permalink of the restricted content).
  • $post_id: The ID of the post that was originally restricted.
  • $user: The WP_User object of the user who has logged in.

#Examples

#Redirect to PDF viewer for Download Manager files

The example below will redirect users directly to the PDF viewer for Download Manager files instead of to the download page after login.

function wpf_modify_return_after_login_url( $url, $post_id, $user ) {
    
    // Check if this is a Download Manager file
    if ( 'wpdmpro' === get_post_type( $post_id ) ) {
        
        // Instead of returning to the download page, go directly to the PDF viewer
        $pdf_viewer_url = home_url( '/?__wpdm_pdf_viewer=' . $post_id );
        
        return $pdf_viewer_url;
    }
    
    return $url;
}

add_filter( 'wpf_return_after_login_url', 'wpf_modify_return_after_login_url', 10, 3 );

#Role-based redirect after login

Redirect users to different pages based on their WordPress user role after successfully accessing restricted content.

function wpf_role_based_return_redirect( $url, $post_id, $user ) {
    
    // Redirect customers to their account page
    if ( in_array( 'customer', $user->roles ) ) {
        return wc_get_page_permalink( 'myaccount' );
    }
    
    // Redirect subscribers to a welcome page
    if ( in_array( 'subscriber', $user->roles ) ) {
        return home_url( '/welcome/' );
    }
    
    // Redirect administrators to the admin dashboard
    if ( in_array( 'administrator', $user->roles ) ) {
        return admin_url();
    }
    
    return $url;
}

add_filter( 'wpf_return_after_login_url', 'wpf_role_based_return_redirect', 10, 3 );

#Add tracking parameters to return URL

Add UTM parameters or other tracking data to the return URL for analytics purposes.

function wpf_add_tracking_to_return_url( $url, $post_id, $user ) {
    
    $tracking_params = array(
        'utm_source'   => 'wp_fusion',
        'utm_medium'   => 'return_login',
        'utm_campaign' => 'content_access',
        'user_id'      => $user->ID,
        'post_id'      => $post_id
    );
    
    return add_query_arg( $tracking_params, $url );
}

add_filter( 'wpf_return_after_login_url', 'wpf_add_tracking_to_return_url', 10, 3 );

#Redirect to course overview for LearnDash lessons

When users access a restricted LearnDash lesson, redirect them to the course overview page instead of the individual lesson.

function wpf_learndash_course_redirect( $url, $post_id, $user ) {
    
    // Check if this is a LearnDash lesson
    if ( 'sfwd-lessons' === get_post_type( $post_id ) ) {
        
        // Get the associated course
        $course_id = learndash_get_course_id( $post_id );
        
        if ( $course_id ) {
            return get_permalink( $course_id );
        }
    }
    
    return $url;
}

add_filter( 'wpf_return_after_login_url', 'wpf_learndash_course_redirect', 10, 3 );

Was this helpful?