Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

Redirecting after login except for a specific page

You can achieve this by modifying your custom_login_redirect function to include a conditional check based on a parameter you pass from your specific page. Here’s an example of how you can modify your function:

function custom_login_redirect($redirect_to, $request, $user) {
    // Check if there is a user and if the user has roles
    if (isset($user->roles) && is_array($user->roles)) {
      // Check if the login request comes from your specific page
      if (isset($_GET['from']) && $_GET['from'] == 'special-page') {
        // Do not redirect
        return $redirect_to;
       } else {
        // Redirect to '/main_report' or another specified page
        return home_url('/main_report');
      }
    } else {
      // Default redirect if there's no user 
      return $redirect_to;
    }
}
add_filter('login_redirect', 'custom_login_redirect', 10, 3);

If modifying the login form is not possible, or if you prefer not to use GET parameters for tracking the origin of a login request, you can use PHP session variables as an alternative. This method involves setting a session variable when a user visits your specific page and then checking this variable during the login process.

function set_login_origin_session() {
if (is_page('your-specific-page-slug')) {
    // Start the session if it hasn't already been started
    if (!session_id()) {
        session_start();
    }
    // Set a session variable to indicate the user came from the specific page
    $_SESSION['login_origin'] = 'specific-page';
   }
}
add_action('template_redirect', 'set_login_origin_session');

function custom_login_redirect($redirect_to, $request, $user) {
  // Check if the session variable is set and equals 'specific-page'
  if (isset($_SESSION['login_origin']) && $_SESSION['login_origin'] === 'specific-page') {
    // Clear the session variable to prevent unintended redirects in future logins
    unset($_SESSION['login_origin']);
    
    // Do not redirect; return the original destination
    return $redirect_to;
  } else {
    // Redirect to your default location
    return home_url('/main_report');
  }
}
add_filter('login_redirect', 'custom_login_redirect', 10, 3);

Related Posts:

  1. Masking logout URL
  2. How can I tell if I’m on a login page? [duplicate]
  3. redirect wp-login.php to another page
  4. How do I redirect upon login a specific user based on role?
  5. How can I get my Script to work on the Login page?
  6. Login Redirect if Logged in from Specific Page
  7. Get User Login Data (date, time… )
  8. Registration Hooks don’t appear to be working
  9. Logout Redirect and also WP-login.php Redirect
  10. How to redirect Subscribers on login to specific page, when logging in from a Page
  11. How to request login for user but not for bots
  12. wp_enqueue_script was called incorrectly
  13. Issues with title-tag and document_title_parts
  14. Import WordPress XML File from Within Functions.php
  15. Define custom Page Template without its own .php file
  16. Does hooking into the same action multiple times drain memory?
  17. How to influence the information displayed on widget inside wp-admin
  18. Extract image from content and set it as the featured image
  19. Is there a hook or function I can use to display all theme files being used on a current page?
  20. Issue with get_theme_mod returning a blank value instead of the saved value
  21. Add action hook conditionally – only when home.php in use
  22. Show modified time if post is actually modified
  23. why doesnt is_home() work in functions.php
  24. How to create thumbnails for PDF uploads?
  25. Logout/login redirect CSS issue
  26. Implementing DNS Prefetching with WordPress
  27. Detecting post type within init action
  28. Help me to understand wp_header() and wp_footer() functions
  29. Is it possible to use a forgot password url filter?
  30. In WooCommerce I need to modify the thankyou.php page [closed]
  31. Change the footer text on the login page
  32. How can I add an extra WooCommerce hook
  33. Why get_header() or get_footer() does not run twice if called in the same php file?
  34. Paged Single Post doesn’t redirect to the main url after Removing Pagination
  35. Remove Page Title from Static Frontpage
  36. How to override WordPress registration and insert an auto-generated username?
  37. Set default options for inserting media
  38. after login that will redirect user role into a page
  39. Change parent theme file function in child themes functions.php
  40. Adding extra SVGs to TwentyNineteen child theme using class TwentyNineteen_SVG_Icons
  41. How to hook into the quick edit action?
  42. gform_after_submission content appears immediately after , not in post body [closed]
  43. Login using the password from protected pages
  44. How do I change parameters without changing the core
  45. Is it possible to be more page/post specific with admin_enqueue_script?
  46. Logging in redirects to correct page but shows logged out content until forced refresh
  47. WooCommerce add_action hook results in 500 error
  48. Issue adding text after short description on product pages Woocommerce [closed]
  49. wordpress custom login successful redirect hook
  50. Customize “the_posts_pagination” and put list instead div
  51. Remove default user registration, login and subscriber profiles
  52. Increment price for Woocommerce Minicart [closed]
  53. how to change link of some wordpress pages
  54. Gravity Forms field entries into wp_query loop [closed]
  55. Post thumbnail relative link and HTML modify
  56. Firing a function AFTER redirect
  57. Proper way of using functions in action hook?
  58. is_user_logged_in not working to redirect only logged out users
  59. How to preserve edits to Name or Slug of term when using wp_update_term on save?
  60. Accepted arguments value in hook functions
  61. Get current page_id before loop, in functions.php
  62. Changing “Lost Password Email Link” to custom password reset page
  63. Members only site – still need the lost password page accessible
  64. How to properly add function called by action-hooked function to functions.php?
  65. Which action hook to use for function?
  66. Change size and crop medium_large images
  67. Gravity Forms – Using a Form to Pre-populate A Gravity Form [closed]
  68. Ninja form Redirect depending on text field content [closed]
  69. Redirect specific page in WordPress for first time visit
  70. Running a script before absolutely everything
  71. How do I redirect all 404 error url to Subcategory url
  72. Inserting a functions output after the content
  73. How to display post content instead of excerpt
  74. Add function to every post?
  75. How to change login labels
  76. Warning: call_user_func_array() expects parameter 1 to be a valid callback, function ‘greenday_head’ not found or invalid function name
  77. Modify a function without editing template
  78. Don’t delete a page if it holds users
  79. Problem only while using require_once() within functions.php
  80. Passing values from a widget to a function within a plugin
  81. Which action does wp_update_user triggers?
  82. Trigger a custom function when option are saved in admin area
  83. Execute a ultimate member action when user role is updated
  84. Add other social networks to TwentyNineteen_SVG_Icons class in child theme?
  85. How to use wp_trash_post (or wp_delete_post) for deleting a (or all) post(s) from custom post type?
  86. 404 redirect to previous category
  87. What did I do wrong in my functions code, that will not change the “Get New Password” text to “Send It”?
  88. Unable to login using username
  89. Replace a menu with widget or a custom template file programmatically
  90. When a user logs in, how can they view the website instead of the admin menu?
  91. Redirect to another page if the user is logged in when pressing again the login button on menu bar
  92. Footer disappears when using the_content ( )
  93. WordPress PHP Conflicting User Sessions
  94. Incorrect redirect after commenting
  95. jQuery does not work
  96. Why does re-using this function not work? [closed]
  97. Pass parameter to hooked function using custom page template
  98. Override the WordPress core function wp_referer_field
  99. How to return hook data when multiple parameters are present?
  100. Hook into a function without a hook?
Categories functions Tags functions, hooks, login, redirect
Filter on one post type with taxonimy and get other post type
Error counting posts of category

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress