I recently created a wordpress theme, and wanted to have my own login area on the main page. The code I’m using is below, but for some reason it doesn’t quite work. It allows me to log in, but when I do I’m redirected to example.com/wordpress/wordpress/ instead of example.com/wordpress/ and I can’t figure out where the extra subdirectory is coming from. The code ‘works’ because if I manually change the URL after getting 404’d I’m logged in, but I can’t figure out why it’s doing that in the first place.
<?php $args = array(
'echo' => false,
'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 );
echo '<form name="' . $args['form_id'] . '" id="' . $args['form_id'] . '" action="' . esc_url( site_url( 'wp-login.php', 'login_post' ) ) . '" method="post">';
?>
<table>
<tr>
<td>Username</td>
<td>Password</td>
<td></td>
</tr>
<tr>
<td><?php echo '<input type="text" name="log" id="' . esc_attr( $args['id_username'] ) . '" class="input" value="' . esc_attr( $args['value_username'] ) . '" size="12" />'; ?></td>
<td><?php echo '<input type="password" name="pwd" id="' . esc_attr( $args['id_password'] ) . '" class="input" value="" size="12" />'; ?></td>
<td><?php echo '<input type="submit" name="wp-submit" id="' . esc_attr( $args['id_submit'] ) . '" class="button-primary" value="' . esc_attr( $args['label_log_in'] ) . '" />
<input type="hidden" name="redirect_to" value="' . esc_url( $args['redirect'] ) . '" />'; ?></td>
</tr>
</table>
</form>
I’m assuming you have WordPress installed to
http://www.example.com/wordpress
. The problem in your code is theredirect
parameter of your$args
array. You have:'redirect' => site_url( $_SERVER['REQUEST_URI'] ),
The
site_url
function will return the base URL that you have configured in WordPress, in this casehttp://www.example.com/wordpress
, and to the end of that it will add to it the content of$_SERVER['REQUEST_URI']
, which in this case is/wordpress
, so you end up withhttp://www.example.com/wordpress/wordpress
.To solve this issue the parameter you want to pass to
redirect
is whatever URL underneath the/wordpress
directory you want to redirect to. Example:'redirect' => site_url( '/some-example-page/' ),
will redirect to
http://www.example.com/wordpress/some-example-page/
, and'redirect' => site_url(),
will redirect to the base of your WordPress installation,
http://www.example.com/wordpress
.If you remove the
redirect
parameter from your$args
array, the default value ofredirect
will be whatever page the function was called from.See site_url and wp_login_form on the WordPress Codex for more information on the functions, and this Stack Overflow answer for how
$_SERVER['REQUEST_URI']
works. I think you’re using the example from the WordPress Codex page aboutwp_login_form
and the redirect arguments provided on that page will cause this behavior if WordPress isn’t installed to the root of the domain.