3

I have a strange request, but hopefully it's doable. I want to be able to have several logos, for my clients, on my site. Depending on what logo they click it takes them to the login page with the username already entered. So, if they click on the "Bob's Grill" logo, it takes them to the login page with the "Bob's Grill" username already entered.

Is there any way to do this? Thanks!

4
  • 1
    You can pass such things using query args. Take a look at it in Codex, then improve your question. As it currently stands, the answer would simply be Yes. Thanks.
    – kaiser
    Sep 24, 2012 at 13:02
  • 1
    @kaiser are you sure you could use add_query_arg for this? To mee it seems to only pass $_GET params when in wp-login.php the username is checked by $_POST params.
    – janw
    Sep 24, 2012 at 13:10
  • There're multiple ways to get around. Just add what you've already tried. We then will help further.
    – kaiser
    Sep 24, 2012 at 13:14
  • @kaiser I honestly didn't know what to try. I don't have a whole lot of experience with coding stuff. I'm going to try tbuteler's suggestion. I'll post back if it works or if I have problems. Sep 24, 2012 at 13:40

1 Answer 1

2

There are ways to trick WP into pre-populating the username field, but I'm pretty sure it would involve using POST and also tricking it into thinking there were errors in a previous submission.

My advice would be to just use Javascript (or jQuery, as I've done below). It's still 'hacky', but at least it gets the job done with very little overhead and it's dead easy to use. It adds a little script that checks for the prepopulate URL parameter, and triggers autocompletion if it finds anything.

// Add jQuery if prepopulate GET is found
add_action('login_head', 'prepopulate_username_js');
function prepopulate_username_js() {
    if(isset($_GET['prepopulate'])) :
    ?>
    <script type="text/javascript">
        jQuery(function($){
            $('#user_login').val('<?php echo($_GET['prepopulate']); ?>');
        });
    </script>
    <?php
        endif;
}

Then, when creating the links on the logos, simply use something along these lines in your theme's templates to trigger the script above:

<a href="<?php echo(add_query_arg(array('prepopulate' => 'bob'), wp_login_url())); ?>">This will link to a prepopulated login form!</a> // change 'bob' for whatever is relevant
5
  • I'm sorry tbutler, I'm a noobie. I've added the script to my theme, but I don't know how to implement the add_query_arg into a link in a post. How would that be done? Thank you so much for your help. Oct 8, 2012 at 15:05
  • It's just basic PHP using a WordPress function, so you'd use it on your template files. I'm assuming you have control over the code which generate those links, so instead of doing just wp_login_url() or even hardcoding example.com/wp-login.php, you'd put the code I've written above. If you're still unsure about what to do, edit your question with the code that's generating the link (regardless of being yours or a plugin which you can't edit) and we'll try to help you out. Oct 8, 2012 at 17:27
  • @tbutler I created a .js file with the above script and placed it into the js folder for my theme. I then called the script to load in the head of my theme. Is that the correct way to set it up? I then tried to put this code into my theme: <?php add_query_arg(array('prepopulate' => 'bob'), wp_login_url()); ?> Is that correct? Thanks! Oct 9, 2012 at 19:06
  • The first part of the answer is PHP and JS. If you put it all on a separate JS file, it won't work. You could do it, with some modifications, but it's easier just to copy and paste the first part in your functions.php. The second part is correct, yes, but you're missing an 'echo' statement -- without it you won't see any output, although the logic is correct (I've edited the answer to avoid any confusion with this in the future) -- and it would still have to be within an anchor tag or something... Oct 10, 2012 at 1:37
  • @tbutler Awesome! Thank you so much! I got it all set-up and it's working beautiful. You've been a great help! Now I can make client login easier for my clients, as they are not computer literate at all. Once again, thank you so much! Oct 10, 2012 at 17:45

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.