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

Need help with AJAX login to call php in functions.php to handle redirects based on user cap (role)

So, i managed to achieve what i want by directing the exisiting window.location.href to a newly created php file i put in the root called .login.php. I will share the code below as it handles the user roles nicely when logging in using this jquery method

.login.php

<?php
require_once('wp-load.php');

function user_role_check_modal( $role, $user_id = null ) {

  $user = wp_get_current_user();

    if ( empty( $user ) )
    return false;

  return in_array( $role, (array) $user->roles );
}

if ( user_role_check_modal( 'administrator' )):
   header("Location: /wp-admin/");
   exit();

endif;

if ( user_role_check_modal( 'customer' )):
   header("Location: /my-account/");
   exit();

else:

   header("Location: /my-account/");
   exit();

endif;

Hope this helps someone who maybe in the future needs work around

Related Posts:

  1. Adding “Remember Me” in custom login
  2. username_exists() function can’t be access without logging in
  3. How build a custom login/register form with error handling?
  4. PHP – redirect https to http and www to non-www
  5. Change the footer text on the login page
  6. Redirect to Page after Post Submit
  7. Can i check if user is doing any ajax request?
  8. How to redirect users to custom lostpassword page?
  9. How to turn off redirection from ‘domain.com/login’ to ‘domain.com/wp-login.php’
  10. How to add default images into theme customizer image control?
  11. Is the regular ajax request method safe or I should use admin-ajax.php?
  12. Constructing a custom login form using ajax
  13. Validate a users email address when using gmail to register
  14. Logout and redirect using the WP Menu
  15. Click loads template via ajax
  16. Change links automatically to affiliate links
  17. How to speed up admin-ajax.php in wordpress
  18. PHP If user is logged in & on home page redirect
  19. get post based on category chosen in drop down – The ajax method
  20. Login/logout in header
  21. How to store data from multiple forms using ajax and php
  22. PHP 8, AJAX mail form to function.php doesn’t work
  23. Ajax request not sending to server and returning – wp-admin/admin-ajax.php 400
  24. Why ajax doesn’t work on certain wordpress hooks?
  25. Is it possible to intercept all ajax requests and get the parameters and the returns?
  26. dynamic dependent select dropdown
  27. getting logged in user info (wp-load.php) from parent directory
  28. how to make custom ajax handler?
  29. Problem with login form
  30. How to disable controls in theme customizer?
  31. Log in / Log Out Custom Button
  32. How to redirect users based on role and content of redirect_to?
  33. Moving code from theme header to functions.php
  34. How to display login form anywhere, when user isn’t logged in, without redirecting?
  35. is_user_logged_in() not working in AJAX validation call
  36. Using data sent via AJAX in multiple functions on a WP plugin
  37. Function won’t run onclick using Ajax
  38. WordPress redirect redirecting too many times or not at all
  39. Simple AJAX notification when the new post is added to the database
  40. Why i can’t get custom fields value or post ID via Ajax?
  41. Post not populating for custom post type based on category selection
  42. How to get post category list as select in front-end?
  43. Is there a hack for using is_page() within the function.php file?
  44. Execute Jquery when a specific page in my plugin is loading
  45. Ajax URLs without #!, how to prevent falling into single.php on load or reload?
  46. AJAX function not working [closed]
  47. WP insert post Redirect after function has executed
  48. How to define a rule in functions.php that ONLY disables the default wp functionality that undesirably changes ‘&’ to ‘#038;’?
  49. Disclaimer that will show every refresh of the page
  50. Database entry removed on browser refresh, Ajax PHP jQuery
  51. Can’t log in to WordPress wp-admin after adding code to functions.php
  52. Store ajax data in PHP variable
  53. Multiple AJAX handler functions conflict in functions.php
  54. Ajax filter with loadmore button
  55. How to change this ajax function to submit to the default wordpress content area instead of the custom field ‘seller notes’?
  56. Having trouble creating two shortcodes, one for logged in user and one for visitors
  57. register_block_type is not working properly
  58. get_the_author_meta( ‘ID’ ) just return 1
  59. Update $wpdb query with AJAX
  60. Restrict wordpress access to logged users only
  61. Can an AJAX callback function access a PHP variable which was defined during initial page load?
  62. Infinite loop when logging out using custom login form
  63. Menu not updating for logged in users after redirect
  64. how to use auth_redirect() redirect visitor to login page if they are not login when they click account and order page?
  65. Refresh page after login with litespeed cache
  66. WordPress Ajax filter: Create two loops for different output styles?
  67. Offset with ajax load more posts duplicates
  68. ajaxurl usage for a custom function
  69. How to call a function from functions.php with ajax?
  70. wp-login.php?redirect_to=https problem
  71. is_user_logged_in() not working in homepage
  72. Custom Post type Ajax search results
  73. Find the method which AJAX GET calls
  74. AJAX Returning Way Too Many Posts
  75. redirect 301 with special character like WIX site “#!”
  76. wordpress ajax return 0
  77. Submitting my form to the database and then redirecting to payment site
  78. wp_delete_comment is doing nothing
  79. Modify category archive page loop on functions.php
  80. Registration form not registering First and Last name
  81. Modify php code to pass a page id as a parameter in order to create a breadcrumb
  82. Redirect users not logged in to the standard login page (and back) from some posts and pages
  83. ajax form function error
  84. Link to external page has wordpress blog-url inside
  85. AJAX: WordPress filters inside $html do not work as intended
  86. wp_login_url always redirects me to wp-admin
  87. wp-login – unable to redirect user to a custom login url
  88. I got this error POST https://localhost/meraboilerwp/[object%20Object] 404 (Not Found)
  89. How can I display a Divi content inside a modal based on an AJAX request
  90. Redirect to current URL and append specified URL parameter on unsuccessful login through Elementor login form widget
  91. Processing data and redirecting with query string
  92. Removing unneccessary p-tags (Not every p)
  93. How to remove the excerpt in the Dzonia Lite theme
  94. comments_number display outside specified span
  95. Choose To Display Post Views With An Options Panel
  96. Prevent Data Resubmission On Page refresh
  97. Set WordPress Default Template
  98. Recovering WP Login Credintials in Code?
  99. Shortcode content filter?
  100. How to run JS, PHP and etc. inside WP post?
Categories PHP Tags ajax, functions, login, php, redirect
Woocommerce Single Product Tabs Got Duplicate
Display Search Results by tag_ID in my search.php

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