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

Login Redirect if Logged in from Specific Page

You could try something like this to add a custom form field:

add_action('login_form', 'custom_login_field');
function custom_login_field() {
    if (!is_singular('course')) {
        echo "<input type="hidden" name="context" value="notcourse">";
    }
}

Which is then picked up if it is set and does the redirect on login:

add_action('wp_login', 'auto_redirect_after_login');    
function auto_redirect_after_login() {
    if ( (isset($_POST['context'])) && ($_POST['context'] == 'notcourse') ) {
         wp_redirect(home_url()); exit();
    }
}

So that any login form submitted from a non-course page will redirect to home after login as this seems to be what you are intending.

UPDATE

Switching the logic around might work instead:

add_action('login_form', 'custom_login_field');
function custom_login_field() {
    if (is_singular('course')) {
        echo "<input type="hidden" name="context" value="course">";
    }
}

add_action('wp_login', 'auto_redirect_after_login');    
function auto_redirect_after_login() {
    if ( (isset($_POST['context'])) && ($_POST['context'] == 'course') ) {
         return;
    }
    wp_redirect(home_url()); exit();
}

Related Posts:

  1. Masking logout URL
  2. redirect wp-login.php to another page
  3. How do I redirect upon login a specific user based on role?
  4. Logout Redirect and also WP-login.php Redirect
  5. How to redirect Subscribers on login to specific page, when logging in from a Page
  6. How to request login for user but not for bots
  7. Redirecting after login except for a specific page
  8. wp_enqueue_script was called incorrectly
  9. Logout/login redirect CSS issue
  10. How can I tell if I’m on a login page? [duplicate]
  11. Is it possible to use a forgot password url filter?
  12. Paged Single Post doesn’t redirect to the main url after Removing Pagination
  13. Login using the password from protected pages
  14. Logging in redirects to correct page but shows logged out content until forced refresh
  15. wordpress custom login successful redirect hook
  16. Remove default user registration, login and subscriber profiles
  17. is_user_logged_in not working to redirect only logged out users
  18. Members only site – still need the lost password page accessible
  19. Ninja form Redirect depending on text field content [closed]
  20. Redirect specific page in WordPress for first time visit
  21. How do I redirect all 404 error url to Subcategory url
  22. How to change login labels
  23. How can I get my Script to work on the Login page?
  24. 404 redirect to previous category
  25. Unable to login using username
  26. When a user logs in, how can they view the website instead of the admin menu?
  27. Redirect to another page if the user is logged in when pressing again the login button on menu bar
  28. WordPress PHP Conflicting User Sessions
  29. Get User Login Data (date, time… )
  30. Incorrect redirect after commenting
  31. Registration Hooks don’t appear to be working
  32. How i can put $_GET codes in function.php?
  33. If user is logged-in display/hide something
  34. Why does get_template_directory_uri() not include “www”, only for enqueued stylesheets?
  35. Page get parameter doesn’t work with is_single() function
  36. Secondary Menu and Logged In Users
  37. Redirect website domain (including all pages) to external URL after Popup message (few seconds)
  38. Contact Form 7 If Condition
  39. Prevent WordPress Automatic Logout
  40. error at login page in wordpress
  41. Solved: redirect to another page using functions.php
  42. Redirect to other page if login
  43. Add Login/Logout Menu Item to Primary Nav “My Account” Submenu [Woocommerce] [closed]
  44. How to add array [closed]
  45. WordPress website keeps loading the maintenance page
  46. Redirects based on a JSON file
  47. loginout function customization
  48. Add Login and logout buttons to top menu bar
  49. Show errormessages on wrong username/password on custom loginform?
  50. template_redirect action only firing if logged in
  51. WordPress login set cookie that survive browser exit (wp_signon function)
  52. This code is supposed to only allow user to be authenticated if accountVerified is equal to 1, but it still allows user to be authenticated otherwise
  53. Prevent wp_signon redirect on failed login for ajax login
  54. Redirect to one of two pages after data submitted, depending on the current url
  55. How to replace ACTION url from original wordpress login form?
  56. Unable to login after registration
  57. Allow logged in user to view a Page, else send to login screen and then redirect back to Page
  58. Wildcard 301 Redirect Using Theme Function
  59. Competing Login Redirects – Need to be Combined?
  60. Postback redirect through add_action is not triggered
  61. Need help with AJAX login to call php in functions.php to handle redirects based on user cap (role)
  62. Restrict access if logged out except for homepage
  63. Redirect specific author posts to another url
  64. Custom download page
  65. Redirect after login to current URL
  66. How to create a function to redirect a specified page to another page
  67. Login functions
  68. Language switcher for subdomains
  69. WordPress permanently logging users out
  70. Allow the access over wp-login.php
  71. is_user_logged_in() isn’t working
  72. update_user_option not working as expected
  73. “Headers already sent” while trying to add a CSS file to my login page?
  74. Making an under maintenance page (without using plugins)
  75. Template Redirect Function Only Working When Logged In
  76. Disable Attachment Page Except for Category
  77. Simple way to make most of my site private
  78. wp_login_form display no styled form
  79. How to redirect using a custom wordpress api endpoint after form submission?
  80. How to setup a popup registeration page in wordpress with function.php
  81. Overwriting TwentyTwelve template file with child theme template, but lower in the hierarchy
  82. Featured Image keeps cropping
  83. Function Suddenly Stopped Working
  84. Redirect certain pages if user logged in
  85. Prevent creating multiple image resizes in twentytwelve template
  86. Why is this function crashing my wordpress installation? [closed]
  87. Dashboard blocker on network multisite for specific user roles
  88. Shortcode of a function
  89. How to place a div inside a function that creates a div
  90. Echo post title in post
  91. Unable to Change “Pages:” Before WP_LINK_PAGES
  92. colorbox not loading in
  93. dynamic enquiry form [closed]
  94. wp_get_archives() output
  95. I would like single.php to act just like index.php
  96. WP Admin Login keeps redirecting to login
  97. Conditional function for excluding first image from content, not working
  98. Does WordPress has an automatic link parse function?
  99. Fatal error: allowed memory size when after using enqueue_script
  100. The work of WordPress Function update_post
Categories functions Tags functions, login, redirect
Why is server preventing admin styles? (Was: Where are WordPress sessions saved?)
page.php dependent sites are not working in custom woocommerce theme

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