Skip to content

OpenDataRepository/odr-wp-login-redirect

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ODR WordPress Login Redirect

A WordPress plugin that automatically redirects users back to the page they were viewing before login, using cookie-based tracking.

Description

The ODR WordPress Login Redirect plugin enhances the user experience by remembering where users were on your site before they logged in. After successful authentication, users are automatically redirected back to their previous location instead of the default WordPress admin dashboard.

Features

  • Automatic Page Tracking: Uses JavaScript to set a cookie with the current page URL on every visit
  • Smart Redirect: After login, users are redirected to their previous page
  • AJAX Support: Tracks URL hash changes for single-page applications
  • Custom Login Logo: Replace the WordPress logo on the login page with your own image
  • Security First: Validates all redirect URLs to prevent open redirect vulnerabilities
  • Configurable: Admin settings panel for customization
  • Debug Mode: Built-in debugging tools for troubleshooting
  • Exclusion Rules: Automatically excludes login, admin, and password reset pages
  • Custom Exclusions: Add your own pages to exclude from tracking

Installation

  1. Upload the odr-login-redirect folder to the /wp-content/plugins/ directory
  2. Activate the plugin through the 'Plugins' menu in WordPress
  3. Configure settings at Settings > ODR Login Redirect

Configuration

Navigate to Settings > ODR Login Redirect in your WordPress admin to configure:

General Settings

  • Enable Plugin: Toggle the redirect functionality on/off
  • Cookie Expiry: Set how long the redirect cookie remains valid (1-168 hours, default: 24 hours)

Advanced Settings

  • Excluded Pages: Add URLs or URL fragments that should not trigger redirects
  • Debug Mode: Enable console and error log debugging for troubleshooting

How It Works

  1. Page Visit: When a user visits any page (except excluded pages), JavaScript sets a cookie with the current URL
  2. Login Page: When the user reaches the login page, the plugin reads the cookie value
  3. Authentication: After successful login, WordPress redirects to the URL stored in the cookie
  4. Cleanup: The cookie is cleared after use to prevent unwanted future redirects

Cookie Details

  • Name: odr-wordpress-login-redirect
  • Default Expiry: 24 hours
  • Scope: Site-wide (path: /)
  • Security: Uses Secure flag on HTTPS, SameSite=Lax

Excluded Pages

The following pages are automatically excluded from redirect tracking:

  • /wp-login.php
  • /wp-admin/*
  • /wp-register.php
  • /wp-activate.php
  • /wp-signup.php
  • Password reset pages
  • Logout URLs

You can add custom exclusions in the plugin settings.

Security Features

  • Domain Validation: Only redirects to the same domain (prevents open redirect attacks)
  • URL Sanitization: All URLs are properly sanitized and validated
  • Nonce Protection: Admin settings use WordPress nonce verification
  • Secure Cookies: Uses secure cookie flags when on HTTPS

Testing the Plugin

  1. Enable Debug Mode in plugin settings
  2. Open browser developer console (F12)
  3. Visit any page on your site
  4. Check console for "[ODR Login Redirect]" messages
  5. Log out of WordPress
  6. Log back in - you should be redirected to the page from step 3
  7. Check console and PHP error logs for debug information

Troubleshooting

Redirects Not Working

  • Ensure the plugin is enabled in settings
  • Check if JavaScript is enabled in your browser
  • Verify cookies are not being blocked
  • Enable Debug Mode and check console/error logs

Being Redirected to Wrong Page

  • Check the Excluded Pages setting
  • Clear browser cookies and try again
  • Enable Debug Mode to see which URL is being stored

Cookie Not Being Set

  • Check browser console for JavaScript errors
  • Verify the page is not in the excluded list
  • Ensure cookies are enabled in the browser

Developer Information

Hooks and Filters

The plugin uses the following WordPress hooks:

Actions

  • wp_enqueue_scripts - Enqueue public JavaScript
  • login_enqueue_scripts - Add scripts to login page
  • wp_login - Clear cookie after login
  • admin_menu - Add settings page
  • admin_init - Register settings

Filters

  • login_redirect - Handle the actual redirect
  • plugin_action_links_* - Add settings link to plugins page

File Structure

odr-login-redirect/
├── admin/
│   ├── class-odr-wp-login-redirect-admin.php
│   └── partials/
│       └── odr-wp-login-redirect-admin-display.php
├── includes/
│   ├── class-odr-wp-login-redirect.php
│   ├── class-odr-wp-login-redirect-loader.php
│   ├── class-odr-wp-login-redirect-activator.php
│   └── class-odr-wp-login-redirect-deactivator.php
├── public/
│   ├── class-odr-wp-login-redirect-public.php
│   └── js/
│       └── odr-wp-login-redirect-public.js
├── odr-wp-login-redirect.php
├── uninstall.php
└── README.md

Constants

  • ODR_WP_LOGIN_REDIRECT_VERSION - Plugin version
  • ODR_WP_LOGIN_REDIRECT_BASENAME - Plugin basename
  • ODR_WP_LOGIN_REDIRECT_PATH - Plugin directory path
  • ODR_WP_LOGIN_REDIRECT_URL - Plugin directory URL
  • ODR_WP_LOGIN_REDIRECT_COOKIE - Cookie name

Requirements

  • WordPress 5.0 or higher
  • PHP 7.4 or higher
  • JavaScript enabled in browser
  • Cookies enabled in browser

Support

For support or to report issues, please visit opendatarepository.org

License

This plugin is licensed under GPL-2.0+. See LICENSE.txt for details.

Credits

Developed by Open Data Repository

Changelog

1.1.0

  • Added custom login logo feature
  • Users can replace WordPress logo on login page with image from media library
  • Configurable logo dimensions (width/height)
  • Option to link logo to site homepage
  • Added media library selector in admin settings

1.0.1

  • Added hashchange event listener to track URL changes when AJAX content loads
  • Refactored URL exclusion logic to support checking any URL
  • Added support for custom excluded pages/fragments in JavaScript

1.0.0

  • Initial release
  • Cookie-based redirect tracking
  • Admin settings panel
  • Debug mode
  • Security validations

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages