Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I need a PHP page to be secured using wordpress login validation. Therefore I used this piece of code:

$args = array(
    'echo' => true,
    'redirect' => site_url( $_SERVER['REQUEST_URI'] ), 
    'form_id' => 'loginform',
    'label_username' => __( 'Username' ),
    'label_password' => __( 'Password' ),
    'label_remember' => __( 'Remember Me' ),
    'label_log_in' => __( 'Log In' ),
    'id_username' => 'user_login',
    'id_password' => 'user_pass',
    'id_remember' => 'rememberme',
    'id_submit' => 'wp-submit',
    'remember' => true,
    'value_username' => NULL,
    'value_remember' => false );

wp_login_form( $args );

but the redirect doesn't work. When I login as admin user, I get redirected to the wp-admin page. When I login as a user, I get redirected to the front page. :-S

so, I tried this:

function login_redirect( $redirect_to, $request, $user ){
    return home_url(site_url( $_SERVER['REQUEST_URI'] ));
}

add_filter( 'login_redirect', 'login_redirect', 10, 3 );

Didn’t work…

so I gave it a try with this (http://codex.wordpress.org/Plugin_API/Filter_Reference/login_redirect)

Complete code:

<?php
header( 'Cache-Control: no-store, no-cache, must-revalidate' );
header( 'Cache-Control: post-check=0, pre-check=0', false );
header( 'Expires: Mon, 26 Jul 1997 05:00:00 GMT' );
header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s' ) . ' GMT' );
header( 'Pragma: no-cache' );

require('../wp-load.php' );

get_header();

function my_login_redirect( $redirect_to, $request, $user ){
    //is there a user to check?
    global $user;
    if( isset( $user->roles ) && is_array( $user->roles ) ) {
        //check for admins
        if( in_array( "administrator", $user->roles ) ) {
            // redirect them to the default place
            return $redirect_to;
        } else {
            return home_url();
        }
    }
    else {
        return $redirect_to;
    }
}

add_filter("login_redirect", "my_login_redirect", 10, 3);

if ( is_user_logged_in() ) 
{
    echo "Hello. You are logged in<br>";
    wp_loginout();
}
else
{
    echo "Hello. please login<br>";

    wp_login_form();
    echo"<a href=\"".wp_lostpassword_url( get_permalink() )."\" title=\"Lost Password\">I lost my password</a>";
}

get_footer(); 
?>

still...no success.

I am using WP version 3.8

===========================================

added the functions.php file from the theme

<?php
require_once('inc/Sidebar.php');

register_nav_menu( 'top-menu', 'Top Menu' ); 

/*
 * Extending excerpt
 */
function custom_excerpt_length( $length ) {
    return 350;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );


//removing default "Continue Reading from Excerpt
function sbt_custom_excerpt_more( $output ) {
    return preg_replace('/<a[^>]+>Continue reading.*?<\/a>/i','',$output);
}
add_filter( 'get_the_excerpt', 'sbt_custom_excerpt_more', 20 );

//redirecting all users to dashboard (only subscribers on home)

function redirect_users($user_login, $user) {
    if(!current_user_can("subscriber")) {
        wp_redirect(get_admin_url());
    }
}
add_action('wp_login', 'redirect_users', 1, 2);

function wpse126853_redirect_to_request( $redirect_to, $request, $user ) {
    // instead of using $redirect_to we're redirecting back to $request
    return $request;
}
add_filter( 'login_redirect', 'wpse126853_redirect_to_request', 10, 3 );
?>
share|improve this question
    
The first few lines tell me you want to protect a page and make it available for logged in user only and if that's true you can do it by using is_user_logged_in. and if this is not what you want, then please add some more details on how do you want the above to behave? –  Maruti Mohanty Dec 18 '13 at 10:11
    
Hi Maruti, I actually use 'is_user_logged_in' in the code to check if they are logged in or not. Problem is the redirection after login. When I'm not yet logged in. I can see the "Hello. please login" message. When I'm logged in and manually return to the same page and can see the "Hello. You are logged in" message and the log out link. All I want is, the users to be redirected to the same page as they started instead of the main page. thanks –  user369122 Dec 18 '13 at 10:51
add comment

1 Answer

up vote 0 down vote accepted

You can use the login_redirect filter to do this. One of the parameters is $request, which is the URL the user is coming from

Use the below in your active theme's functions.php file to make this work.

function wpse126853_redirect_to_request( $redirect_to, $request, $user ) {
    // instead of using $redirect_to we're redirecting back to $request
    return $request;
}
add_filter( 'login_redirect', 'wpse126853_redirect_to_request', 10, 3 );

You can use the filter for distinguishing redirect destinations by user roles. Take a look at the documentation for a deeper insight into that.

share|improve this answer
    
Hi Maruti, thanks for giving a hand. I've added function wpse126853_redirect_to_request( $redirect_to, $request, $user ) { // instead of using $redirect_to we're redirecting back to $request return $request; } to the functions.php (in the wp-includes directory) and added add_filter( 'login_redirect', 'wpse126853_redirect_to_request', 10, 3 ); to the php script where I call wp_login_form(); after login I still get redirected to the main page... :-S –  user369122 Dec 18 '13 at 11:52
    
First, where are you adding the above? you need to add it in active theme's functions.php file and also remove add_filter("login_redirect", "my_login_redirect", 10, 3); and the concerned function as you don't need it any more –  Maruti Mohanty Dec 18 '13 at 12:18
    
ah...seems I added it to the wrong file. Now I added to code you gave to /httpdocs/wp-content/themes/thewebsite/functions.php But when I add the function to the functions.php file, something goes wrong and I only see a blank page (logged in or not). Please see the full functions.php file from the theme in my post above. Kind regards, Matthijs –  user369122 Dec 18 '13 at 12:45
    
comment out this line add_action('wp_login', 'redirect_users', 1, 2); and check and in the function wp_redirect should be followed by exit;. For details wp_redirect –  Maruti Mohanty Dec 18 '13 at 12:51
    
added the exit; to function redirect_users($user_login, $user) { if(!current_user_can("subscriber")) { wp_redirect(get_admin_url()); exit; } } and commented out //add_action('wp_login', 'redirect_users', 1, 2); but as soon as I add the wpse126853_redirect_to_request function to the script, I get the blank page error. –  user369122 Dec 18 '13 at 12:59
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.