Peter’s Login Redirect

$5.00

Voulez-vous un rabais? Devenir membre en achetant Membre VIP!
Ce produit ne peut être acheté que par les membres. Pour acheter ce produit, inscrivez-vous en achetant {produits}.


Catégorie: .

Description

Define a set of redirect rules for specific users, users with specific roles, users with specific capabilities, and a blanket rule for all other users. Also, set a redirect URL for post-registration. This is all managed in Settings > Login/logout redirects.

You can use the syntax [variable]username[/variable] in your URLs so that the system will build a dynamic URL upon each login, replacing that text with the user’s username. In addition to username, there is “userslug”, “homeurl”, “siteurl”, “postid-23”, “http_referer” and you can also add your own custom URL “variables”. See Other Notes / How to Extend for documentation.

If you’re using a plugin such as Gigya that bypasses the regular WordPress login redirect process (and only allows one fixed redirect URL), set that plugin to redirect to wp-content/plugins/peters-login-redirect/wplogin_redirect_control.php and set the relevant setting to “Yes” at the bottom of the Settings > Login/Logout redirects page in the WordPress admin panel.

You can add your own code logic before and between any of the plugin’s normal redirect checks if needed. See Other Notes / How to Extend for documentation. Some examples include: redirecting the user based on their IP address; and redirect users to a special page on first login.

This plugin also includes a function rul_register that acts the same as the wp_register function you see in templates (typically producing the Register or Site Admin links in the sidebar), except that it will return the custom defined admin address. rul_register takes three parameters: the “before” code (by default “<li>”), the “after” code (by default “</li>”), and whether to echo or return the result (default is true and thus echo).

HOW TO EXTEND

CUSTOM REDIRECT RULES

You can write your own code logic before any of this plugin’s checks for user-specific, role-specific, and capability-specific redirects, as well as before the fallback redirect URL.

Available filters are:

  • rul_before_user
  • rul_before_role
  • rul_before_capability
  • rul_before_fallback

Each takes the same 4 parameters:

  • $custom_redirect_to: This is set as false in case you don’t have any redirect URL to set. Return this instead of false in case you have multiple filters running.
  • $redirect_to: Set by WordPress, usually the admin URL.
  • $requested_redirect_to: Set by WordPress, usually an override set in a GET parameter.
  • $user: A PHP object representing the current user.

Your return value in your own code logic should be the URL to redirect to, or $custom_redirect_to to continue the plugin’s normal checks.

An example of plugin code to redirect users on first login. See http://www.theblog.ca/wordpress-redirect-first-login for standalone functionality:

// Send new users to a special page
function redirectOnFirstLogin( $custom_redirect_to, $redirect_to, $requested_redirect_to, $user )
{
    // URL to redirect to
    $redirect_url = 'http://yoursite.com/firstloginpage';
    // How many times to redirect the user
    $num_redirects = 1;
    // If implementing this on an existing site, this is here so that existing users don't suddenly get the "first login" treatment
    // On a new site, you might remove this setting and the associated check
    // Alternative approach: run a script to assign the "already redirected" property to all existing users
    // Alternative approach: use a date-based check so that all registered users before a certain date are ignored
    // 172800 seconds = 48 hours
    $message_period = 172800;

    /*
        Cookie-based solution: captures users who registered within the last n hours
        The reason to set it as "last n hours" is so that if a user clears their cookies or logs in with a different browser,
        they don't get this same redirect treatment long after they're already a registered user
    */
    /*

    $key_name = 'redirect_on_first_login_' . $user->ID;

    if( strtotime( $user->user_registered ) > ( time() - $message_period )
        && ( !isset( $_COOKIE[$key_name] ) || intval( $_COOKIE[$key_name] ) < $num_redirects )
      )
    {
        if( isset( $_COOKIE[$key_name] ) )
        {
            $num_redirects = intval( $_COOKIE[$key_name] ) + 1;
        }
        setcookie( $key_name, $num_redirects, time() + $message_period, COOKIEPATH, COOKIE_DOMAIN );
        return $redirect_url;
    }
    */
    /*
        User meta value-based solution, stored in the database
    */
    $key_name = 'redirect_on_first_login';
    // Third parameter ensures that the result is a string
    $current_redirect_value = get_user_meta( $user->ID, $key_name, true );
    if( strtotime( $user->user_registered ) > ( time() - $message_period )
        && ( '' == $current_redirect_value || intval( $current_redirect_value ) < $num_redirects )
      )
    {
        if( '' != $current_redirect_value )
        {
            $num_redirects = intval( $current_redirect_value ) + 1;
        }
        update_user_meta( $user->ID, $key_name, $num_redirects );
        return $redirect_url;
    }
    else
    {
        return $custom_redirect_to;
    }
}

add_filter( 'rul_before_user', 'redirectOnFirstLogin', 10, 4 );

An example of plugin code to redirect to a specific URL for only a specific IP range as the first redirect check:

function redirectByIP( $custom_redirect_to, $redirect_to, $requested_redirect_to, $user )
{
    $ip_check = '192.168.0';
    if( 0 === strpos( $_SERVER['REMOTE_ADDR'], $ip_check ) )
    {
        return '/secret_area';
    }
    else
    {
        return $custom_redirect_to;
    }
}

add_filter( 'rul_before_user', 'redirectByIP', 10, 4 );

Note that the same extensibility is available for logout redirects with these filters:

  • rul_before_user_logout
  • rul_before_role_logout
  • rul_before_capability_logout
  • rul_before_fallback_logout

It takes 3 parameters:

  • $custom_redirect_to: This is set as false in case you don’t have any redirect URL to set. Return this instead of false in case you have multiple filters running.
  • $requested_redirect_to: A redirect parameter set via POST or GET.
  • $user: A PHP object representing the current user.

CUSTOM VARIABLE PARAMETERS

There is an available filter “rul_replace_variable” for adding your own custom variable names. For example, to replace [variable]month[/variable] in the redirect URL with the numeric representation of the current month (with leading zeros):

function customRULVariableMonth( $custom_redirect_to, $variable, $user )
{
    if( 'month' == $variable )
    {
        return date( 'm' );
    }
    else
    {
        return $custom_redirect_to;
    }
}

add_filter( 'rul_replace_variable', 'customRULVariableMonth', 10, 3 );

Be sure to rawurlencode the returned variable if necessary.

CUSTOM “MY PORTAL” LINK

A common need is to display the “redirect” link for a user in the site navigation or sidebar.

Look at the function rul_register() in the plugin file for inspiration; it makes use of the redirect_to_front_page() function to determine the URL and then provides the relevant output code.

For a deeper dive into this feature, please see this video

Screenshots

FAQ

Please visit the plugin page at http://www.theblog.ca/wplogin-redirect with any questions.

Login redirects not working? This plugin uses WordPress’s standard login_redirect hook. The usual cause of problems is that another plugin is using the hook first, or there is a custom login form that isn’t even running through the standard WordPress login functions.

These threads might be useful:

  • http://www.theblog.ca/peter-forum/peters-login-redirect/logout-works-great-but-log-in-keeps-going-to-my-account-pages
  • http://www.theblog.ca/peter-forum/peters-login-redirect/redirect-not-working
  • http://www.theblog.ca/peter-forum/peters-login-redirect/any-way-to-solve-re-direct-conflict-with-wp-affiliate-plugin

Also see the instructions at the bottom of the settings on the “Settings > Login/logout redirects” page in the WordPress admin panel that says:

“Use external redirect file. Set this to “Yes” if you are using a plugin such as Gigya that bypasses the regular WordPress redirect process (and allows only one fixed redirect URL). Then, set the redirect URL in the other plugin to
http://www.yoursite.com/wp-content/plugins/peters-login-redirect/wplogin_redirect_control.php”