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.

Evening,

I'm trying to make a simple 'login' feature for my site. I'd rather just have a dropdown box of sorts, and to login, the redirect back to the page you submitted it on.

I've some code I found (I'm VERY new to wordpress), and it doesn't seem to be working right, when I click 'send', nothing changes. The page gives no errors, and it just sits there, it doesn't say I've logged in, or give any indication it's acting different.

<?php if (!(current_user_can('level_0'))){ ?>
<h2>Login</h2>
<form action="<?php echo get_option('home'); ?>../../wordpress/wp-login.php" method="post">
<input type="text" name="log" id="log" value="<?php echo wp_specialchars(stripslashes($user_login), 1) ?>" size="20" />
<input type="password" name="pwd" id="pwd" size="20" />
<input type="submit" name="submit" value="Send" class="button" />
    <p>
       <label for="rememberme"><input name="rememberme" id="rememberme" type="checkbox" checked="checked" value="forever" /> Remember me</label>
       <input type="hidden" name="redirect_to" value="<?php echo $_SERVER['REQUEST_URI']; ?>" />
    </p>
</form>
<a href="<?php echo get_option('home'); ?>/wp-login.php?action=lostpassword">Recover password</a>
<?php } else { ?>
<h2>Logout</h2>
<a href="<?php echo wp_logout_url(urlencode($_SERVER['REQUEST_URI'])); ?>">logout</a><br />
<a href="http://XXX/wp-admin/">admin</a>
<?php }?>
share|improve this question
    
Please can you explain what isn't working correctly? –  Vince Pettit Jul 17 '12 at 14:58
    
Yeah, sorry, when I click 'send', nothing changes. The page gives no errors, and it just sits there, it doesn't say I've logged in, or give any indication it's acting different. –  Adola Jul 17 '12 at 15:05
add comment

2 Answers 2

It depends on where your login script is. This is my testscript (saved as ìndex.php`):

<html>
<head>
    <title>Testform</title>
</head>
<body>
    <?php
        require_once '../wp-load.php';

        var_dump($_COOKIE);

        if ( !( current_user_can('level_0') ) ){
            wp_login_form();
        }
        else { ?>
        <ul>
            <li><a href="<?php echo home_url('/wp-admin/'); ?>">Admin</a></li>
            <li><a href="<?php echo wp_logout_url( get_permalink() ); ?>">Logout</a></li>
        </ul>
    <?php } ?>
</body>

I installed WordPress in a subdirectory on my local server (testdoma.in/wordpress). If I install the script in a subdirectory within my WordPress installation (testdoma.in/wordpress/external), it works. If I install the script in the root (and change the require_once to wordpress/wp-load.php), no way to get it working.

If you try to access WordPress functions, the scripts have to be in a subfolder of or directly within your WordPress installation.

UPDATE

After some research, it is a cookie problem. The plugin root Cookie will resolve the problem.

share|improve this answer
add comment

Change this line:

<form action="<?php echo get_option('home'); ?>../../wordpress/wp-login.php" method="post">

to this:

<form action="<?php echo site_url('/wp-login.php') ?>" method="post">

Updated:

Consider to use simple wp_login_form(). See wp_login_form reference for possible function arguments.

<?php
if ( !( current_user_can('level_0') ) ){
    wp_login_form();
}
else { ?>
<ul>
    <li><a href="<?php echo site_url('/wp-admin/'); ?>">Admin</a></li>
    <li><a href="<?php echo wp_logout_url( get_permalink() ); ?>">Logout</a></li>
</ul>
<?php } ?>
share|improve this answer
    
That didn't change anything, after hitting 'send' the page returns, and doesn't indicate a change. IS this a cookie error? –  Adola Jul 18 '12 at 0:57
    
@Adola, please see updated code above. –  Max Yudin Jul 18 '12 at 7:13
    
The same issue persists. After hitting 'login' the page returns without any indication of loggin in. It just greets me with the login form again. –  Adola Jul 18 '12 at 10:22
    
Do you have debugging enabled? WP_DEBUG. And where do you place this form? –  Max Yudin Jul 18 '12 at 10:33
    
I do not have debugging enabled, and I placed it in /domain/TEST/scripts/php/testWP.php Also, I suspect it's a cookie issue, as in, is_user_logged_in() isn't working right. –  Adola Jul 18 '12 at 10:38
show 7 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.