Im trying to redirect same page using wp_login_form(). this is my code

function loginform() {
if ( is_user_logged_in()){
echo '<p> <a  href="'.esc_url(home_url('/my-dashboard/')).'" >'.esc_html( __( 'Thank you.! Please Go to dashboard', 'mytheme' )).'</a></p>';}
$args = array('redirect' => home_url(), 'id_username' => 'user','id_password' => 'pass',);
wp_login_form( $args );}}
add_shortcode('login-form', 'loginform'); 

i chancged the default wp-login.php url by using this filter

add_filter( 'login_url', 'new_login_page', 10, 3 );
function new_login_page( $login_url, $redirect, $force_reauth ) {$login_page = esc_url(home_url( '/login/'));return add_query_arg( 'redirect_to', $redirect, $login_page );}

then called to non logged in users to log in current page they are in by adding this code to

echo '<a href="'. esc_url(wp_login_url( get_permalink())) .'"><span class="icon" ></span>'.esc_html( __( 'login', 'mytheme' )).'</a> '; 

everything work , but after logged in it wont redirect to previous page.. any help? highly appreciate.. thank you :)

1 Answer 1


after logged in it wont redirect to previous page

It's because in your loginform() function, the 'redirect' => home_url() below sets the redirect URL to the homepage URL:

$args = array('redirect' => home_url(), 'id_username' => 'user','id_password' => 'pass',);

So if you want to redirect to the previous page, then remove the redirect argument above, or use the code below, which means if redirect_to=<URL> is present in the current URL as in example.com/login/?redirect_to=https://example.com/some-page/, then the specified URL will be used instead:

// Define your $args like this:
$args = array( 'id_username' => 'user', 'id_password' => 'pass' );

// Then add the 'redirect', if the URL has the redirect_to=<URL>.
if ( ! empty( $_REQUEST['redirect_to'] ) ) {
    $args['redirect'] = $_REQUEST['redirect_to'];

See the wp_login_form() documentation for more details on the redirect and other arguments.

Additional Notes

Excerpt from codex.wordpress.org/Shortcode_API:

The return value of a shortcode handler function is inserted into the post content output in place of the shortcode macro. Remember to use return and not echo - anything that is echoed will be output to the browser, but it won't appear in the correct place on the page.

Therefore your loginform() should actually return the output and not echoing it (or anything else), because in addition to the above issue (shortcode appearing in the wrong place), an echo in the function would result in a failure in saving a post via the block editor (Gutenberg) which uses the REST API (so if your function echo something, the REST API response will be invalid).

So in that function, change the echo '<p> to return '<p>, then in the $args array, add 'echo' => false, and finally change the wp_login_form( $args ) to return wp_login_form( $args ).

  • 1
    Dear @Sally CJ. thank you very much for the Answer of my question. yes its worked :) . i really appreciate your help..! and Big thanks for the Instruction above . i learn lot form it..! thank you :)
    – chamboy
    Commented Aug 8, 2021 at 13:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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