0

I do have a php website running on a certain domain, and I have installed Wordpress on a subdomain. I need to put a login form on the homepage (not the subdomain) where the user will fill in the username and password, then get logged in and redirected automatically to the Wordpress dashboard on the subdomain. But I'm a newbie, could anyone tell me how to achieve it? Thank you very much.

2

You can add the static form code to your main site's code (your domain in this case is example.com and your subdomain is sub.example.com):

<form name="loginform" id="loginform" action="https://sub.example.com/wp-login.php" method="post">
            <p>
                <label for="user_login">Username or Email Address</label>
                <input type="text" name="log" id="user_login" class="input" value="" size="20" autocapitalize="off">
            </p>

            <div class="user-pass-wrap">
                <label for="user_pass">Password</label>
                <div class="wp-pwd">
                    <input type="password" name="pwd" id="user_pass" class="input password-input" value="" size="20">
                    <button type="button" class="button button-secondary wp-hide-pw hide-if-no-js" data-toggle="0" aria-label="Show password">
                        <span class="dashicons dashicons-visibility" aria-hidden="true"></span>
                    </button>
                </div>
            </div>
                        <p class="forgetmenot"><input name="rememberme" type="checkbox" id="rememberme" value="forever"> <label for="rememberme">Remember Me</label></p>
            <p class="submit">
                <input type="submit" name="wp-submit" id="wp-submit" class="button button-primary button-large" value="Log In">
                                    <input type="hidden" name="redirect_to" value="https://sub.example.com/wp-admin/">
                                    <input type="hidden" name="testcookie" value="1">
            </p>
        </form>

The hidden redirect_to input field tells WP where it will redirect upon successful login (wp-admin/ will have you end up in the dashboard).

The form however will not look like it does in WordPress due to the missing style. You can either add the WP login.css to your main site's code

<link rel="stylesheet" id="login-css" href="https://sub.example.com/wp-admin/css/login.css?ver=5.3.2" type="text/css" media="all">

or just style it the way you like.

-1

There are several ways to achieve what you are looking for, but I am going to cover two in this answer:

WordPress, No Custom Coding (via Multisite)

A WordPress multisite gives you the ability to have two separate websites utilizing the same WordPress installation. You can set up your main domain (example.com) as the main multisite domain. Then, create a sub-site (or child site) that is your actual site (test.example.com). The subdomain site will contain your content.

Lock both sites down by requiring login (https://wordpress.org/plugins/wp-force-login/). Then set up the login page on the core site (example.com) and set a custom redirect after login to your subdomain (test.example.com). When the user logs in to one multisite site, they login across the network (this is also how WordPress.com works). So if they authenticate on the main site, the subsite is also logged in and you can redirect them there.

On the subsite, require login and redirect them to the main site login page (example.com). That way, if they are no longer logged in but visit your subsite, they will be redirected to the login page.

Additional reading:

WordPress, Custom Code Method

WordPress has a way to authenticate users programatically. Utilizing the wp_set_current_user() and wp_set_auth_cookie() functions, you can authenticate a user via a PHP page on the main domain (example.com) and then redirect them to WordPress for authentication and processing.

Additional reading:

Note: this method would require you to still authenticate the user without WordPress. I would recommend writing an API call that reaches out to WordPress and confirms the user credentials are authentic before redirecting them and "auto-logging them in" via the above functions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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