Plugin Directory

Changeset 3333657


Ignore:
Timestamp:
07/24/2025 01:12:40 PM (6 months ago)
Author:
handyplugins
Message:

Update to version 2.0 from GitHub

Location:
primary-redirect
Files:
10 added
8 deleted
5 edited
1 copied

Legend:

Unmodified
Added
Removed
  • primary-redirect/assets/banner-772x250.png

    • Property svn:mime-type changed from application/octet-stream to image/png
  • primary-redirect/tags/2.0/primary-redirect.php

    r590604 r3333657  
    11<?php
    2 /*
    3 Plugin Name: Primary Redirect
    4 Plugin URI: http://blog.uysalmustafa.com/primary-redirect/
    5 Description: Redirects users to special url or their primary blog's dashboard after they've logged in  replacing the default 'go to dashboard' behavior.
    6 Author: Mustafa Uysal
    7 Version:1.0
    8 Text Domain: primary_redirect
    9 Domain Path: /languages/
    10 Author URI: http://blog.uysalmustafa.com
    11 License: GPLv2 (or later)
    12 Network: true
    13 */
    14 
    15 
     2/**
     3 * Plugin Name: Primary Redirect
     4 * Plugin URI: https://handyplugins.co
     5 * Description: Redirects users to a custom URL or their primary blog's dashboard after login, replacing the default WordPress behavior.
     6 * Version: 2.0
     7 * Requires at least: 5.0
     8 * Requires PHP: 7.4
     9 * Author: HandyPlugins
     10 * Author URI: https://handyplugins.co/
     11 * License: GPL v2 or later
     12 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
     13 * Text Domain: primary-redirect
     14 * Domain Path: /languages
     15 *
     16 * @package PrimaryRedirect
     17 */
     18
     19// Prevent direct access.
     20if ( ! defined( 'ABSPATH' ) ) {
     21    exit;
     22}
     23
     24// Define plugin constants.
     25define( 'PRIMARY_REDIRECT_VERSION', '2.0' );
     26define( 'PRIMARY_REDIRECT_PLUGIN_FILE', __FILE__ );
     27define( 'PRIMARY_REDIRECT_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
     28define( 'PRIMARY_REDIRECT_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
     29
     30/**
     31 * Main Primary Redirect Class
     32 *
     33 * @since 2.0.0
     34 */
    1635class Primary_Redirect {
    1736
    18 
    19     /**
    20      * PHP 5 constructor
    21      **/
    22     function __construct() {
    23         add_filter( 'login_redirect', array( &$this, 'redirect' ), 10, 3 );
    24         add_action( 'wpmu_options', array( &$this, 'network_option' ) );
    25         add_action( 'update_wpmu_options', array( &$this, 'update_network_option' ) );
    26         add_action( 'admin_init', array( &$this, 'add_settings_field' ) );
    27 
    28     }
    29    
    30     //get locale
    31     function plugin_localization() { 
    32         load_plugin_textdomain( 'primary_redirect', false, '/primary-redirect/languages/' );
    33     }
    34    
    35     /**
    36      * Redirect user on login
    37      **/
    38     function redirect( $redirect_to, $requested_redirect_to, $user ) {
    39     global $wpdb;
    40     $primary_redirection = get_site_option( 'primary_dashboard_true' );
    41        
    42         if(($primary_redirection == 1)&&($user->ID != 0)){ 
    43                 $user_info = get_userdata($user->ID);
    44             if ($user_info->primary_blog) {
    45                 $primary_url = get_blogaddress_by_id($user_info->primary_blog) . 'wp-admin/';
    46                 if ($primary_url) {
    47                     wp_redirect($primary_url);
    48                 exit();
    49             }
    50         }
    51         }   
    52     else{
    53        
     37    /**
     38     * Plugin instance.
     39     *
     40     * @since 2.0.0
     41     * @var Primary_Redirect
     42     */
     43    private static $instance = null;
     44
     45    /**
     46     * Get plugin instance.
     47     *
     48     * @since 2.0.0
     49     * @return Primary_Redirect
     50     */
     51    public static function get_instance() {
     52        if ( null === self::$instance ) {
     53            self::$instance = new self();
     54        }
     55
     56        return self::$instance;
     57    }
     58
     59    /**
     60     * Constructor.
     61     *
     62     * @since 2.0.0
     63     */
     64    private function __construct() {
     65        $this->init();
     66    }
     67
     68    /**
     69     * Initialize the plugin.
     70     *
     71     * @since 2.0.0
     72     */
     73    private function init() {
     74        add_action( 'plugins_loaded', array( $this, 'load_textdomain' ) );
     75        add_filter( 'login_redirect', array( $this, 'handle_login_redirect' ), 10, 3 );
     76
     77        if ( is_multisite() ) {
     78            add_action( 'wpmu_options', array( $this, 'render_network_settings' ) );
     79            add_action( 'update_wpmu_options', array( $this, 'update_network_settings' ) );
     80        } else {
     81            add_action( 'admin_init', array( $this, 'register_single_site_settings' ) );
     82        }
     83    }
     84
     85    /**
     86     * Load plugin textdomain.
     87     *
     88     * @since 2.0.0
     89     */
     90    public function load_textdomain() {
     91        load_plugin_textdomain(
     92            'primary-redirect',
     93            false,
     94            dirname( plugin_basename( __FILE__ ) ) . '/languages/'
     95        );
     96    }
     97
     98    /**
     99     * Handle login redirect.
     100     *
     101     * @since 2.0.0
     102     * @param string  $redirect_to           The redirect destination URL.
     103     * @param string  $requested_redirect_to The requested redirect destination URL passed as a parameter.
     104     * @param WP_User $user                  The WP_User object for the user being redirected.
     105     * @return string The redirect URL.
     106     */
     107    public function handle_login_redirect( $redirect_to, $requested_redirect_to, $user ) {
     108        // Don't redirect if there's an error with the user.
     109        if ( is_wp_error( $user ) ) {
     110            return $redirect_to;
     111        }
     112
     113        // Don't redirect for interim login or reauth.
    54114        $interim_login = isset( $_REQUEST['interim-login'] );
    55         $reauth = empty( $_REQUEST['reauth'] ) ? false : true;
    56 
    57         if( $this->is_plugin_active_for_network( plugin_basename( __FILE__ ) ) )
    58             $primary_redirect_url = get_site_option( 'primary_redirect_url' );
    59         else
    60             $primary_redirect_url = get_option( 'primary_redirect_url' );
    61 
    62         if ( !is_wp_error( $user ) && !$reauth && !$interim_login && !empty( $primary_redirect_url ) ) {
    63             wp_redirect( $primary_redirect_url );
    64             exit();
    65         }
    66 }
     115        $reauth = ! empty( $_REQUEST['reauth'] );
     116
     117        if ( $interim_login || $reauth ) {
     118            return $redirect_to;
     119        }
     120
     121        // Check if primary dashboard redirect is enabled (multisite only).
     122        if ( is_multisite() && $this->is_primary_dashboard_redirect_enabled() ) {
     123            $primary_redirect_url = $this->get_primary_dashboard_url( $user );
     124            if ( $primary_redirect_url ) {
     125                return $primary_redirect_url;
     126            }
     127        }
     128
     129        // Check for custom redirect URL.
     130        $custom_redirect_url = $this->get_custom_redirect_url();
     131        if ( ! empty( $custom_redirect_url ) ) {
     132            return esc_url_raw( $custom_redirect_url );
     133        }
     134
    67135        return $redirect_to;
    68136    }
    69    
    70    
    71 
    72     /**
    73      * Network option
    74      **/
    75     function network_option() {
    76         if( ! $this->is_plugin_active_for_network( plugin_basename( __FILE__ ) ) )
    77             return;
     137
     138    /**
     139     * Check if primary dashboard redirect is enabled.
     140     *
     141     * @since 2.0.0
     142     * @return bool
     143     */
     144    private function is_primary_dashboard_redirect_enabled() {
     145        return '1' === get_site_option( 'primary_redirect_dashboard_enabled', '0' );
     146    }
     147
     148    /**
     149     * Get primary dashboard URL for user.
     150     *
     151     * @since 2.0.0
     152     * @param WP_User $user The user object.
     153     * @return string|false Primary dashboard URL or false if not available.
     154     */
     155    private function get_primary_dashboard_url( $user ) {
     156        if ( ! is_multisite() || ! isset( $user->primary_blog ) ) {
     157            return false;
     158        }
     159
     160        $primary_blog_id = absint( $user->primary_blog );
     161        if ( $primary_blog_id <= 0 ) {
     162            return false;
     163        }
     164
     165        $primary_url = get_blogaddress_by_id( $primary_blog_id );
     166        if ( $primary_url ) {
     167            return trailingslashit( $primary_url ) . 'wp-admin/';
     168        }
     169
     170        return false;
     171    }
     172
     173    /**
     174     * Get custom redirect URL.
     175     *
     176     * @since 2.0.0
     177     * @return string
     178     */
     179    private function get_custom_redirect_url() {
     180        if ( is_multisite() ) {
     181            return get_site_option( 'primary_redirect_url', '' );
     182        }
     183
     184        return get_option( 'primary_redirect_url', '' );
     185    }
     186
     187    /**
     188     * Render network settings.
     189     *
     190     * @since 2.0.0
     191     */
     192    public function render_network_settings() {
     193        $redirect_url = get_site_option( 'primary_redirect_url', '' );
     194        $dashboard_enabled = get_site_option( 'primary_redirect_dashboard_enabled', '0' );
    78195        ?>
    79         <h3><?php _e( 'Primary Redirect', 'primary_redirect' ); ?></h3>
     196        <h3><?php esc_html_e( 'Primary Redirect Settings', 'primary-redirect' ); ?></h3>
    80197        <table class="form-table">
    81             <tr valign="top">
    82                 <th scope="row"><label for="primary_redirect_url"><?php _e( 'Redirect to', 'primary_redirect' ) ?></label></th>
    83                 <td style="width:280px;">
    84                     <input name="primary_redirect_url" type="text" id="primary_redirect_url" value="<?php echo esc_attr( get_site_option( 'primary_redirect_url' ) ) ?>" size="40" />
    85                     <br />
    86                     <?php _e( 'The URL users will be redirected to after login.', 'primary_redirect' ) ?>
     198            <tr>
     199                <th scope="row">
     200                    <label for="primary_redirect_url"><?php esc_html_e( 'Custom Redirect URL', 'primary-redirect' ); ?></label>
     201                </th>
     202                <td>
     203                    <input
     204                        name="primary_redirect_url"
     205                        type="url"
     206                        id="primary_redirect_url"
     207                        value="<?php echo esc_attr( $redirect_url ); ?>"
     208                        class="regular-text"
     209                        placeholder="https://example.com/dashboard"
     210                    />
     211                    <p class="description">
     212                        <?php esc_html_e( 'Enter a custom URL to redirect users after login. Leave empty to use WordPress default behavior.', 'primary-redirect' ); ?>
     213                    </p>
    87214                </td>
     215            </tr>
     216            <tr>
     217                <th scope="row">
     218                    <?php esc_html_e( 'Primary Dashboard Redirect', 'primary-redirect' ); ?>
     219                </th>
    88220                <td>
    89                     <input type="checkbox" name="primary_dashboard_true" value="1" <?php if ( get_site_option( 'primary_dashboard_true') == '1') { echo 'checked="checked"';} ?> /><?php _e( 'Redirect to Primary Dashboard' , 'primary_redirect' ); ?><br>
    90                    
     221                    <label>
     222                        <input
     223                            type="checkbox"
     224                            name="primary_redirect_dashboard_enabled"
     225                            value="1"
     226                            <?php checked( $dashboard_enabled, '1' ); ?>
     227                        />
     228                        <?php esc_html_e( 'Redirect users to their primary blog dashboard (overrides custom URL)', 'primary-redirect' ); ?>
     229                    </label>
     230                    <p class="description">
     231                        <?php esc_html_e( 'This option is only available in multisite installations.', 'primary-redirect' ); ?>
     232                    </p>
    91233                </td>
    92234            </tr>
     
    96238
    97239    /**
    98      * Save option in the option
    99      **/
    100     function update_network_option() {
    101         update_site_option( 'primary_redirect_url', stripslashes( $_POST['primary_redirect_url'] ) );
    102         update_site_option( 'primary_dashboard_true', stripslashes( $_POST['primary_dashboard_true'] ) );
    103        
    104     }
    105 
    106     /**
    107      * Add setting field for singlesite
    108      **/
    109     function add_settings_field() {
    110         if( $this->is_plugin_active_for_network( plugin_basename( __FILE__ ) ) )
     240     * Update network settings.
     241     *
     242     * @since 2.0.0
     243     */
     244    public function update_network_settings() {
     245        // Verify user capabilities.
     246        if ( ! current_user_can( 'manage_network_options' ) ) {
    111247            return;
    112 
    113         add_settings_section( 'primary_redirect_setting_section', __( 'Primary Redirect', 'primary_redirect' ), '__return_false', 'general' );
    114 
    115         add_settings_field( 'primary_redirect_url', __( 'Redirect to', 'primary_redirect' ), array( &$this, 'site_option' ), 'general', 'primary_redirect_setting_section' );
    116        
    117         register_setting( 'general', 'primary_redirect_url' );
    118        
    119     }
    120 
    121     /**
    122      * Setting field for singlesite
    123      **/
    124     function site_option() {
    125         echo '<input name="primary_redirect_url" type="text" id="primary_redirect_url" value="' . esc_attr( get_option( 'primary_redirect_url' ) ) . '" size="40" />';
    126     }
    127 
    128     /**
    129      * Verify if plugin is network activated
    130      **/
    131     function is_plugin_active_for_network( $plugin ) {
    132         if ( !is_multisite() )
    133             return false;
    134 
    135         $plugins = get_site_option( 'active_sitewide_plugins');
    136         if ( isset($plugins[$plugin]) )
    137             return true;
    138 
    139         return false;
    140     }
    141 
     248        }
     249
     250        // Sanitize and update redirect URL.
     251        $redirect_url = isset( $_POST['primary_redirect_url'] ) ? esc_url_raw( wp_unslash( $_POST['primary_redirect_url'] ) ) : '';
     252        update_site_option( 'primary_redirect_url', $redirect_url );
     253
     254        // Update dashboard redirect setting.
     255        $dashboard_enabled = isset( $_POST['primary_redirect_dashboard_enabled'] ) ? '1' : '0';
     256        update_site_option( 'primary_redirect_dashboard_enabled', $dashboard_enabled );
     257    }
     258
     259    /**
     260     * Register single site settings.
     261     *
     262     * @since 2.0.0
     263     */
     264    public function register_single_site_settings() {
     265        add_settings_section(
     266            'primary_redirect_settings',
     267            __( 'Primary Redirect Settings', 'primary-redirect' ),
     268            array( $this, 'render_settings_section_description' ),
     269            'general'
     270        );
     271
     272        add_settings_field(
     273            'primary_redirect_url',
     274            __( 'Redirect URL after login', 'primary-redirect' ),
     275            array( $this, 'render_single_site_setting_field' ),
     276            'general',
     277            'primary_redirect_settings'
     278        );
     279
     280        register_setting(
     281            'general',
     282            'primary_redirect_url',
     283            array(
     284                'type'              => 'string',
     285                'sanitize_callback' => 'esc_url_raw',
     286                'default'           => '',
     287            )
     288        );
     289    }
     290
     291    /**
     292     * Render settings section description.
     293     *
     294     * @since 2.0.0
     295     */
     296    public function render_settings_section_description() {
     297        echo '<p>' . esc_html__( 'Configure where users should be redirected after logging in.', 'primary-redirect' ) . '</p>';
     298    }
     299
     300    /**
     301     * Render single site setting field.
     302     *
     303     * @since 2.0.0
     304     */
     305    public function render_single_site_setting_field() {
     306        $redirect_url = get_option( 'primary_redirect_url', '' );
     307        ?>
     308        <input
     309            name="primary_redirect_url"
     310            type="url"
     311            id="primary_redirect_url"
     312            value="<?php echo esc_attr( $redirect_url ); ?>"
     313            class="regular-text"
     314            placeholder="https://example.com/dashboard"
     315        />
     316        <p class="description">
     317            <?php esc_html_e( 'Enter a custom URL to redirect users after login. Leave empty to use WordPress default behavior.', 'primary-redirect' ); ?>
     318        </p>
     319        <?php
     320    }
    142321}
    143322
    144 
    145 $primary_redirect =& new Primary_Redirect();
    146 
    147 ?>
     323/**
     324 * Initialize the plugin.
     325 *
     326 * @since 2.0.0
     327 */
     328function primary_redirect_init() {
     329    Primary_Redirect::get_instance();
     330}
     331
     332// Initialize the plugin.
     333add_action( 'init', 'primary_redirect_init' );
  • primary-redirect/tags/2.0/readme.txt

    r651332 r3333657  
    11=== Primary Redirect ===
    2 Contributors: m_uysl,LettoBlog
    3 Tags: multisite,login,redirect,redirection,primary,primary redirect,wpmudev
    4 Requires at least: 3.1
    5 Tested up to: 3.5.1
    6 Stable tag: 1.0
    7 License: GPLv2 (or later)
     2Contributors: handyplugins, m_uysl
     3Tags: multisite, login, redirect, redirection, primary
     4Requires at least: 5.0
     5Tested up to: 6.4
     6Requires PHP: 7.4
     7Stable tag: 2.0
     8License: GPLv2 or later
     9License URI: https://www.gnu.org/licenses/gpl-2.0.html
    810
    9 Redirects users to their primary blog's dashboard or special page.
     11Redirects users to a custom URL or their primary blog's dashboard after login, replacing the default WordPress behavior.
    1012
    1113== Description ==
    12 Redirects users to special url or their primary blog's dashboard after they've logged in  replacing the default 'go to dashboard' behavior.
    13 It's inspired by the WPMU DEV login redirect plugin.
    1414
    15 = Translations =
     15Primary Redirect is a powerful WordPress plugin that allows you to customize where users are redirected after logging in. Instead of the default WordPress behavior, you can redirect users to:
    1616
    17 * English (en\_US), built-in
    18 * Turkish (tr\_TR), native support
     17* A custom URL of your choice
     18* Their primary blog's dashboard (in multisite installations)
     19
     20This plugin is perfect for:
     21* Multisite networks where you want users to go to their primary blog
     22* Sites with custom dashboards or landing pages
     23* Improving user experience with personalized redirects
     24
     25= Key Features =
     26
     27* **Custom URL Redirect**: Set any URL as the post-login destination
     28* **Primary Blog Redirect**: Automatically redirect users to their primary blog's dashboard (multisite)
     29* **Network & Single Site Support**: Works on both multisite networks and single WordPress sites
     30* **Easy Configuration**: Simple settings interface in WordPress admin
     31* **Developer Friendly**: Clean, modern code following WordPress best practices
     32
     33= Multisite Support =
     34
     35On multisite installations, you get additional options:
     36* Redirect users to their primary blog's dashboard
     37* Network-wide settings that apply to all sites
     38* Override custom URLs with primary blog redirect
     39
     40= Single Site Support =
     41
     42On single WordPress sites, configure a custom redirect URL that applies to all users after login.
    1943
    2044== Installation ==
    2145
    22 Extract the zip file and just drop the contents in the wp-content/plugins/ directory of your WordPress installation and then activate the Plugin from Network admin's Plugins page.
     461. Upload the plugin files to `/wp-content/plugins/primary-redirect/` directory, or install through WordPress admin
     472. Activate the plugin through the 'Plugins' screen in WordPress
     483. For multisite: Configure settings in Network Admin > Settings
     494. For single site: Configure settings in Settings > General
     50
    2351== Frequently Asked Questions ==
    2452
    25 *   Is it compatible with single site?
    26     - Yes. 
    27 *   Is it support special url redirection?
    28     - Yes. If you're using multisite you can choose primary redirection or enter special url.
    29    
     53= Does this work with single WordPress sites? =
     54
     55Yes! The plugin works on both single WordPress installations and multisite networks.
     56
     57= Can I redirect different users to different URLs? =
     58
     59Currently, the plugin redirects all users to the same custom URL or their primary blog dashboard. User-specific redirects are not supported in this version.
     60
     61= Does this affect interim logins or authentication flows? =
     62
     63No, the plugin respects WordPress's interim login and reauth processes and won't interfere with them.
     64
     65= Is this compatible with other login plugins? =
     66
     67The plugin uses WordPress's standard `login_redirect` filter, so it should be compatible with most other plugins. However, if another plugin also modifies login redirects, the last one to run may take precedence.
     68
    3069== Screenshots ==
    31 1. screenshot-1.png
    32 2. screenshot-2.png
     70
     711. Network admin settings for multisite installations
     722. Single site settings in General options
    3373
    3474== Changelog ==
    3575
     76= 2.0 (Jul 24, 2025) =
     77* Complete rewrite with modern WordPress best practices
     78* Improved security with proper input sanitization and capability checks
     79* Better code organization and documentation
     80* Updated plugin header and branding for HandyPlugins
     81* Improved user interface with better descriptions
     82* Added proper URL validation
     83* Enhanced multisite support
     84* Minimum WordPress version: 5.0
     85* Minimum PHP version: 7.4
     86
     87= 1.1 =
     88* Serbo-Croatian language pack added
     89
    3690= 1.0 =
     91* Initial release
    3792
    38 Initial release.
     93== Upgrade Notice ==
     94
     95= 2.0 =
     96Major update with improved security, modern code, and better user experience. Please test in a staging environment before updating production sites.
  • primary-redirect/trunk/primary-redirect.php

    r590604 r3333657  
    11<?php
    2 /*
    3 Plugin Name: Primary Redirect
    4 Plugin URI: http://blog.uysalmustafa.com/primary-redirect/
    5 Description: Redirects users to special url or their primary blog's dashboard after they've logged in  replacing the default 'go to dashboard' behavior.
    6 Author: Mustafa Uysal
    7 Version:1.0
    8 Text Domain: primary_redirect
    9 Domain Path: /languages/
    10 Author URI: http://blog.uysalmustafa.com
    11 License: GPLv2 (or later)
    12 Network: true
    13 */
    14 
    15 
     2/**
     3 * Plugin Name: Primary Redirect
     4 * Plugin URI: https://handyplugins.co
     5 * Description: Redirects users to a custom URL or their primary blog's dashboard after login, replacing the default WordPress behavior.
     6 * Version: 2.0
     7 * Requires at least: 5.0
     8 * Requires PHP: 7.4
     9 * Author: HandyPlugins
     10 * Author URI: https://handyplugins.co/
     11 * License: GPL v2 or later
     12 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
     13 * Text Domain: primary-redirect
     14 * Domain Path: /languages
     15 *
     16 * @package PrimaryRedirect
     17 */
     18
     19// Prevent direct access.
     20if ( ! defined( 'ABSPATH' ) ) {
     21    exit;
     22}
     23
     24// Define plugin constants.
     25define( 'PRIMARY_REDIRECT_VERSION', '2.0' );
     26define( 'PRIMARY_REDIRECT_PLUGIN_FILE', __FILE__ );
     27define( 'PRIMARY_REDIRECT_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
     28define( 'PRIMARY_REDIRECT_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
     29
     30/**
     31 * Main Primary Redirect Class
     32 *
     33 * @since 2.0.0
     34 */
    1635class Primary_Redirect {
    1736
    18 
    19     /**
    20      * PHP 5 constructor
    21      **/
    22     function __construct() {
    23         add_filter( 'login_redirect', array( &$this, 'redirect' ), 10, 3 );
    24         add_action( 'wpmu_options', array( &$this, 'network_option' ) );
    25         add_action( 'update_wpmu_options', array( &$this, 'update_network_option' ) );
    26         add_action( 'admin_init', array( &$this, 'add_settings_field' ) );
    27 
    28     }
    29    
    30     //get locale
    31     function plugin_localization() { 
    32         load_plugin_textdomain( 'primary_redirect', false, '/primary-redirect/languages/' );
    33     }
    34    
    35     /**
    36      * Redirect user on login
    37      **/
    38     function redirect( $redirect_to, $requested_redirect_to, $user ) {
    39     global $wpdb;
    40     $primary_redirection = get_site_option( 'primary_dashboard_true' );
    41        
    42         if(($primary_redirection == 1)&&($user->ID != 0)){ 
    43                 $user_info = get_userdata($user->ID);
    44             if ($user_info->primary_blog) {
    45                 $primary_url = get_blogaddress_by_id($user_info->primary_blog) . 'wp-admin/';
    46                 if ($primary_url) {
    47                     wp_redirect($primary_url);
    48                 exit();
    49             }
    50         }
    51         }   
    52     else{
    53        
     37    /**
     38     * Plugin instance.
     39     *
     40     * @since 2.0.0
     41     * @var Primary_Redirect
     42     */
     43    private static $instance = null;
     44
     45    /**
     46     * Get plugin instance.
     47     *
     48     * @since 2.0.0
     49     * @return Primary_Redirect
     50     */
     51    public static function get_instance() {
     52        if ( null === self::$instance ) {
     53            self::$instance = new self();
     54        }
     55
     56        return self::$instance;
     57    }
     58
     59    /**
     60     * Constructor.
     61     *
     62     * @since 2.0.0
     63     */
     64    private function __construct() {
     65        $this->init();
     66    }
     67
     68    /**
     69     * Initialize the plugin.
     70     *
     71     * @since 2.0.0
     72     */
     73    private function init() {
     74        add_action( 'plugins_loaded', array( $this, 'load_textdomain' ) );
     75        add_filter( 'login_redirect', array( $this, 'handle_login_redirect' ), 10, 3 );
     76
     77        if ( is_multisite() ) {
     78            add_action( 'wpmu_options', array( $this, 'render_network_settings' ) );
     79            add_action( 'update_wpmu_options', array( $this, 'update_network_settings' ) );
     80        } else {
     81            add_action( 'admin_init', array( $this, 'register_single_site_settings' ) );
     82        }
     83    }
     84
     85    /**
     86     * Load plugin textdomain.
     87     *
     88     * @since 2.0.0
     89     */
     90    public function load_textdomain() {
     91        load_plugin_textdomain(
     92            'primary-redirect',
     93            false,
     94            dirname( plugin_basename( __FILE__ ) ) . '/languages/'
     95        );
     96    }
     97
     98    /**
     99     * Handle login redirect.
     100     *
     101     * @since 2.0.0
     102     * @param string  $redirect_to           The redirect destination URL.
     103     * @param string  $requested_redirect_to The requested redirect destination URL passed as a parameter.
     104     * @param WP_User $user                  The WP_User object for the user being redirected.
     105     * @return string The redirect URL.
     106     */
     107    public function handle_login_redirect( $redirect_to, $requested_redirect_to, $user ) {
     108        // Don't redirect if there's an error with the user.
     109        if ( is_wp_error( $user ) ) {
     110            return $redirect_to;
     111        }
     112
     113        // Don't redirect for interim login or reauth.
    54114        $interim_login = isset( $_REQUEST['interim-login'] );
    55         $reauth = empty( $_REQUEST['reauth'] ) ? false : true;
    56 
    57         if( $this->is_plugin_active_for_network( plugin_basename( __FILE__ ) ) )
    58             $primary_redirect_url = get_site_option( 'primary_redirect_url' );
    59         else
    60             $primary_redirect_url = get_option( 'primary_redirect_url' );
    61 
    62         if ( !is_wp_error( $user ) && !$reauth && !$interim_login && !empty( $primary_redirect_url ) ) {
    63             wp_redirect( $primary_redirect_url );
    64             exit();
    65         }
    66 }
     115        $reauth = ! empty( $_REQUEST['reauth'] );
     116
     117        if ( $interim_login || $reauth ) {
     118            return $redirect_to;
     119        }
     120
     121        // Check if primary dashboard redirect is enabled (multisite only).
     122        if ( is_multisite() && $this->is_primary_dashboard_redirect_enabled() ) {
     123            $primary_redirect_url = $this->get_primary_dashboard_url( $user );
     124            if ( $primary_redirect_url ) {
     125                return $primary_redirect_url;
     126            }
     127        }
     128
     129        // Check for custom redirect URL.
     130        $custom_redirect_url = $this->get_custom_redirect_url();
     131        if ( ! empty( $custom_redirect_url ) ) {
     132            return esc_url_raw( $custom_redirect_url );
     133        }
     134
    67135        return $redirect_to;
    68136    }
    69    
    70    
    71 
    72     /**
    73      * Network option
    74      **/
    75     function network_option() {
    76         if( ! $this->is_plugin_active_for_network( plugin_basename( __FILE__ ) ) )
    77             return;
     137
     138    /**
     139     * Check if primary dashboard redirect is enabled.
     140     *
     141     * @since 2.0.0
     142     * @return bool
     143     */
     144    private function is_primary_dashboard_redirect_enabled() {
     145        return '1' === get_site_option( 'primary_redirect_dashboard_enabled', '0' );
     146    }
     147
     148    /**
     149     * Get primary dashboard URL for user.
     150     *
     151     * @since 2.0.0
     152     * @param WP_User $user The user object.
     153     * @return string|false Primary dashboard URL or false if not available.
     154     */
     155    private function get_primary_dashboard_url( $user ) {
     156        if ( ! is_multisite() || ! isset( $user->primary_blog ) ) {
     157            return false;
     158        }
     159
     160        $primary_blog_id = absint( $user->primary_blog );
     161        if ( $primary_blog_id <= 0 ) {
     162            return false;
     163        }
     164
     165        $primary_url = get_blogaddress_by_id( $primary_blog_id );
     166        if ( $primary_url ) {
     167            return trailingslashit( $primary_url ) . 'wp-admin/';
     168        }
     169
     170        return false;
     171    }
     172
     173    /**
     174     * Get custom redirect URL.
     175     *
     176     * @since 2.0.0
     177     * @return string
     178     */
     179    private function get_custom_redirect_url() {
     180        if ( is_multisite() ) {
     181            return get_site_option( 'primary_redirect_url', '' );
     182        }
     183
     184        return get_option( 'primary_redirect_url', '' );
     185    }
     186
     187    /**
     188     * Render network settings.
     189     *
     190     * @since 2.0.0
     191     */
     192    public function render_network_settings() {
     193        $redirect_url = get_site_option( 'primary_redirect_url', '' );
     194        $dashboard_enabled = get_site_option( 'primary_redirect_dashboard_enabled', '0' );
    78195        ?>
    79         <h3><?php _e( 'Primary Redirect', 'primary_redirect' ); ?></h3>
     196        <h3><?php esc_html_e( 'Primary Redirect Settings', 'primary-redirect' ); ?></h3>
    80197        <table class="form-table">
    81             <tr valign="top">
    82                 <th scope="row"><label for="primary_redirect_url"><?php _e( 'Redirect to', 'primary_redirect' ) ?></label></th>
    83                 <td style="width:280px;">
    84                     <input name="primary_redirect_url" type="text" id="primary_redirect_url" value="<?php echo esc_attr( get_site_option( 'primary_redirect_url' ) ) ?>" size="40" />
    85                     <br />
    86                     <?php _e( 'The URL users will be redirected to after login.', 'primary_redirect' ) ?>
     198            <tr>
     199                <th scope="row">
     200                    <label for="primary_redirect_url"><?php esc_html_e( 'Custom Redirect URL', 'primary-redirect' ); ?></label>
     201                </th>
     202                <td>
     203                    <input
     204                        name="primary_redirect_url"
     205                        type="url"
     206                        id="primary_redirect_url"
     207                        value="<?php echo esc_attr( $redirect_url ); ?>"
     208                        class="regular-text"
     209                        placeholder="https://example.com/dashboard"
     210                    />
     211                    <p class="description">
     212                        <?php esc_html_e( 'Enter a custom URL to redirect users after login. Leave empty to use WordPress default behavior.', 'primary-redirect' ); ?>
     213                    </p>
    87214                </td>
     215            </tr>
     216            <tr>
     217                <th scope="row">
     218                    <?php esc_html_e( 'Primary Dashboard Redirect', 'primary-redirect' ); ?>
     219                </th>
    88220                <td>
    89                     <input type="checkbox" name="primary_dashboard_true" value="1" <?php if ( get_site_option( 'primary_dashboard_true') == '1') { echo 'checked="checked"';} ?> /><?php _e( 'Redirect to Primary Dashboard' , 'primary_redirect' ); ?><br>
    90                    
     221                    <label>
     222                        <input
     223                            type="checkbox"
     224                            name="primary_redirect_dashboard_enabled"
     225                            value="1"
     226                            <?php checked( $dashboard_enabled, '1' ); ?>
     227                        />
     228                        <?php esc_html_e( 'Redirect users to their primary blog dashboard (overrides custom URL)', 'primary-redirect' ); ?>
     229                    </label>
     230                    <p class="description">
     231                        <?php esc_html_e( 'This option is only available in multisite installations.', 'primary-redirect' ); ?>
     232                    </p>
    91233                </td>
    92234            </tr>
     
    96238
    97239    /**
    98      * Save option in the option
    99      **/
    100     function update_network_option() {
    101         update_site_option( 'primary_redirect_url', stripslashes( $_POST['primary_redirect_url'] ) );
    102         update_site_option( 'primary_dashboard_true', stripslashes( $_POST['primary_dashboard_true'] ) );
    103        
    104     }
    105 
    106     /**
    107      * Add setting field for singlesite
    108      **/
    109     function add_settings_field() {
    110         if( $this->is_plugin_active_for_network( plugin_basename( __FILE__ ) ) )
     240     * Update network settings.
     241     *
     242     * @since 2.0.0
     243     */
     244    public function update_network_settings() {
     245        // Verify user capabilities.
     246        if ( ! current_user_can( 'manage_network_options' ) ) {
    111247            return;
    112 
    113         add_settings_section( 'primary_redirect_setting_section', __( 'Primary Redirect', 'primary_redirect' ), '__return_false', 'general' );
    114 
    115         add_settings_field( 'primary_redirect_url', __( 'Redirect to', 'primary_redirect' ), array( &$this, 'site_option' ), 'general', 'primary_redirect_setting_section' );
    116        
    117         register_setting( 'general', 'primary_redirect_url' );
    118        
    119     }
    120 
    121     /**
    122      * Setting field for singlesite
    123      **/
    124     function site_option() {
    125         echo '<input name="primary_redirect_url" type="text" id="primary_redirect_url" value="' . esc_attr( get_option( 'primary_redirect_url' ) ) . '" size="40" />';
    126     }
    127 
    128     /**
    129      * Verify if plugin is network activated
    130      **/
    131     function is_plugin_active_for_network( $plugin ) {
    132         if ( !is_multisite() )
    133             return false;
    134 
    135         $plugins = get_site_option( 'active_sitewide_plugins');
    136         if ( isset($plugins[$plugin]) )
    137             return true;
    138 
    139         return false;
    140     }
    141 
     248        }
     249
     250        // Sanitize and update redirect URL.
     251        $redirect_url = isset( $_POST['primary_redirect_url'] ) ? esc_url_raw( wp_unslash( $_POST['primary_redirect_url'] ) ) : '';
     252        update_site_option( 'primary_redirect_url', $redirect_url );
     253
     254        // Update dashboard redirect setting.
     255        $dashboard_enabled = isset( $_POST['primary_redirect_dashboard_enabled'] ) ? '1' : '0';
     256        update_site_option( 'primary_redirect_dashboard_enabled', $dashboard_enabled );
     257    }
     258
     259    /**
     260     * Register single site settings.
     261     *
     262     * @since 2.0.0
     263     */
     264    public function register_single_site_settings() {
     265        add_settings_section(
     266            'primary_redirect_settings',
     267            __( 'Primary Redirect Settings', 'primary-redirect' ),
     268            array( $this, 'render_settings_section_description' ),
     269            'general'
     270        );
     271
     272        add_settings_field(
     273            'primary_redirect_url',
     274            __( 'Redirect URL after login', 'primary-redirect' ),
     275            array( $this, 'render_single_site_setting_field' ),
     276            'general',
     277            'primary_redirect_settings'
     278        );
     279
     280        register_setting(
     281            'general',
     282            'primary_redirect_url',
     283            array(
     284                'type'              => 'string',
     285                'sanitize_callback' => 'esc_url_raw',
     286                'default'           => '',
     287            )
     288        );
     289    }
     290
     291    /**
     292     * Render settings section description.
     293     *
     294     * @since 2.0.0
     295     */
     296    public function render_settings_section_description() {
     297        echo '<p>' . esc_html__( 'Configure where users should be redirected after logging in.', 'primary-redirect' ) . '</p>';
     298    }
     299
     300    /**
     301     * Render single site setting field.
     302     *
     303     * @since 2.0.0
     304     */
     305    public function render_single_site_setting_field() {
     306        $redirect_url = get_option( 'primary_redirect_url', '' );
     307        ?>
     308        <input
     309            name="primary_redirect_url"
     310            type="url"
     311            id="primary_redirect_url"
     312            value="<?php echo esc_attr( $redirect_url ); ?>"
     313            class="regular-text"
     314            placeholder="https://example.com/dashboard"
     315        />
     316        <p class="description">
     317            <?php esc_html_e( 'Enter a custom URL to redirect users after login. Leave empty to use WordPress default behavior.', 'primary-redirect' ); ?>
     318        </p>
     319        <?php
     320    }
    142321}
    143322
    144 
    145 $primary_redirect =& new Primary_Redirect();
    146 
    147 ?>
     323/**
     324 * Initialize the plugin.
     325 *
     326 * @since 2.0.0
     327 */
     328function primary_redirect_init() {
     329    Primary_Redirect::get_instance();
     330}
     331
     332// Initialize the plugin.
     333add_action( 'init', 'primary_redirect_init' );
  • primary-redirect/trunk/readme.txt

    r651332 r3333657  
    11=== Primary Redirect ===
    2 Contributors: m_uysl,LettoBlog
    3 Tags: multisite,login,redirect,redirection,primary,primary redirect,wpmudev
    4 Requires at least: 3.1
    5 Tested up to: 3.5.1
    6 Stable tag: 1.0
    7 License: GPLv2 (or later)
     2Contributors: handyplugins, m_uysl
     3Tags: multisite, login, redirect, redirection, primary
     4Requires at least: 5.0
     5Tested up to: 6.4
     6Requires PHP: 7.4
     7Stable tag: 2.0
     8License: GPLv2 or later
     9License URI: https://www.gnu.org/licenses/gpl-2.0.html
    810
    9 Redirects users to their primary blog's dashboard or special page.
     11Redirects users to a custom URL or their primary blog's dashboard after login, replacing the default WordPress behavior.
    1012
    1113== Description ==
    12 Redirects users to special url or their primary blog's dashboard after they've logged in  replacing the default 'go to dashboard' behavior.
    13 It's inspired by the WPMU DEV login redirect plugin.
    1414
    15 = Translations =
     15Primary Redirect is a powerful WordPress plugin that allows you to customize where users are redirected after logging in. Instead of the default WordPress behavior, you can redirect users to:
    1616
    17 * English (en\_US), built-in
    18 * Turkish (tr\_TR), native support
     17* A custom URL of your choice
     18* Their primary blog's dashboard (in multisite installations)
     19
     20This plugin is perfect for:
     21* Multisite networks where you want users to go to their primary blog
     22* Sites with custom dashboards or landing pages
     23* Improving user experience with personalized redirects
     24
     25= Key Features =
     26
     27* **Custom URL Redirect**: Set any URL as the post-login destination
     28* **Primary Blog Redirect**: Automatically redirect users to their primary blog's dashboard (multisite)
     29* **Network & Single Site Support**: Works on both multisite networks and single WordPress sites
     30* **Easy Configuration**: Simple settings interface in WordPress admin
     31* **Developer Friendly**: Clean, modern code following WordPress best practices
     32
     33= Multisite Support =
     34
     35On multisite installations, you get additional options:
     36* Redirect users to their primary blog's dashboard
     37* Network-wide settings that apply to all sites
     38* Override custom URLs with primary blog redirect
     39
     40= Single Site Support =
     41
     42On single WordPress sites, configure a custom redirect URL that applies to all users after login.
    1943
    2044== Installation ==
    2145
    22 Extract the zip file and just drop the contents in the wp-content/plugins/ directory of your WordPress installation and then activate the Plugin from Network admin's Plugins page.
     461. Upload the plugin files to `/wp-content/plugins/primary-redirect/` directory, or install through WordPress admin
     472. Activate the plugin through the 'Plugins' screen in WordPress
     483. For multisite: Configure settings in Network Admin > Settings
     494. For single site: Configure settings in Settings > General
     50
    2351== Frequently Asked Questions ==
    2452
    25 *   Is it compatible with single site?
    26     - Yes. 
    27 *   Is it support special url redirection?
    28     - Yes. If you're using multisite you can choose primary redirection or enter special url.
    29    
     53= Does this work with single WordPress sites? =
     54
     55Yes! The plugin works on both single WordPress installations and multisite networks.
     56
     57= Can I redirect different users to different URLs? =
     58
     59Currently, the plugin redirects all users to the same custom URL or their primary blog dashboard. User-specific redirects are not supported in this version.
     60
     61= Does this affect interim logins or authentication flows? =
     62
     63No, the plugin respects WordPress's interim login and reauth processes and won't interfere with them.
     64
     65= Is this compatible with other login plugins? =
     66
     67The plugin uses WordPress's standard `login_redirect` filter, so it should be compatible with most other plugins. However, if another plugin also modifies login redirects, the last one to run may take precedence.
     68
    3069== Screenshots ==
    31 1. screenshot-1.png
    32 2. screenshot-2.png
     70
     711. Network admin settings for multisite installations
     722. Single site settings in General options
    3373
    3474== Changelog ==
    3575
     76= 2.0 (Jul 24, 2025) =
     77* Complete rewrite with modern WordPress best practices
     78* Improved security with proper input sanitization and capability checks
     79* Better code organization and documentation
     80* Updated plugin header and branding for HandyPlugins
     81* Improved user interface with better descriptions
     82* Added proper URL validation
     83* Enhanced multisite support
     84* Minimum WordPress version: 5.0
     85* Minimum PHP version: 7.4
     86
     87= 1.1 =
     88* Serbo-Croatian language pack added
     89
    3690= 1.0 =
     91* Initial release
    3792
    38 Initial release.
     93== Upgrade Notice ==
     94
     95= 2.0 =
     96Major update with improved security, modern code, and better user experience. Please test in a staging environment before updating production sites.
Note: See TracChangeset for help on using the changeset viewer.