Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

shinigaimi's avatar

Sharing login between Laravel & Wordpress

Hey all I am having a tough time I found literally what my question was here is a quote from a previous post Pretty much I have a woocommerce store with subs etc and creating a dashboard for services purchased so they can access what they've purchased.

"We are using WordPress as our store (using woocommerce). our backend use laravel where users who bought our services can use and manage the services they bought from us. we will eventually move to custom implementation of our store in laravel but right now we don't want to break anything. what i want is to integrate wordpress and laravels login so they don't have to login or register twice. something like github use (if you login at github you are automatically logged in at gist). both laravel and wordpress will use different database (or user tables at least). i can create a user in laravel when someone register at wordpress. but i have no clue how to share login session. why both ? we are using different readily available plugins of woocommerce (multiple subscriptions, addons, product variants etc) which will take a lot of time to port to laravel. any help would be great"

and the solution that they gave was given was

Route::get('/wordpress', function(){
require('..\wordpress\wp-load.php');
return loginUser(Auth::user()->username);
});
This part goes in the wordpress function.php 
 function loginFunction($username){
     $user_id = username_exists($username);
     $userdata = get_userdata($user_id);
     $user = set_current_user($user_id,$username);
     wp_set_auth_cookie($user_id);
    do_action('wp_login',$userdata->ID);
    // you can redirect the authenticated user to the "logged-in-page", define('MY_PROFILE_PAGE',1); f.e. first
    header("Location:".get_page_link(MY_PROFILE_PAGE));
}

but when I do this I am given an error when I load the /wordpress route

FatalErrorException in l10n.php line 203:
Cannot redeclare __() (previously declared in C:\wamp64\www\sunfield\vendor\laravel\framework\src\Illuminate\Foundation\helpers.php:848)

This is a brand new project and I am getting this error :( here's a link to the original post: https://laracasts.com/discuss/channels/general-discussion/sharing-login-between-laravel-wordpress?page=1

if you guys know of a much easier way to incorporate this login system across both platforms I would love to hear!

0 likes
4 replies
gabrielbuzzi's avatar

Maybe you duplicate the wordpress database to you laravel or vice versa, and than reset everyone password, so they just need to change it, and add a kind of register in both database when new users comes.

Pretty easy quick fix.

nielsnl's avatar

I had a similar requirement today, and ran into the same solution and error. With some adjustments it will still work though. So for anyone else ending up here:

The key is to use Wordpress' SHORTINIT feature to load a minimal version of Wordpress, then include only those classes that are really needed.

namespace App\Actions;

use App\Models\User;

class WordpressLoginAction
{
    public function execute(User $user)
    {
        define('SHORTINIT', true);
        define('LOGGED_IN_COOKIE',      'wordpress_logged_in_' .md5(config('wordpress.url')));
        define('SECURE_AUTH_COOKIE',    'wordpress_sec_' . md5(config('wordpress.url')));
        define('COOKIEPATH',            '/');
        define('SITECOOKIEPATH',        '/');
        define('ADMIN_COOKIE_PATH',     '/wp-admin');
        define('PLUGINS_COOKIE_PATH',   '/wp-content/plugins');
        define('COOKIE_DOMAIN', false);

        require(config('wordpress.path') . '/wp-load.php');
        require(config('wordpress.path') . '/wp-includes/user.php');
        require(config('wordpress.path') . '/wp-includes/pluggable.php');
        require(config('wordpress.path') . '/wp-includes/kses.php');
        require(config('wordpress.path') . '/wp-includes/class-wp-user.php');
        require(config('wordpress.path') . '/wp-includes/class-wp-role.php');
        require(config('wordpress.path') . '/wp-includes/class-wp-roles.php');
        require(config('wordpress.path') . '/wp-includes/capabilities.php');
        require(config('wordpress.path') . '/wp-includes/class-wp-session-tokens.php');
        require(config('wordpress.path') . '/wp-includes/class-wp-user-meta-session-tokens.php');

        if($user_id = username_exists($user->username)) {
            wp_set_current_user($user_id, $user->username);
            wp_set_auth_cookie($user_id);
        }
    }
}

I now simply call this Action from my LoginController after a successful Auth::attempt:

$wordpressLoginAction->execute(auth()->user());

There's no need to adjust any Wordpress code.

1 like
clubcouleurs's avatar

hello Dear Nielsnl, thanks for this solution, can you give us more information to how implement this ? Thank yo very much

forit8's avatar

Hello, I am a beginner programmer and I know php, laravel .... Now they have given me a project in WordPress and I have to make the login of my Laravel project common for both projects and I don't know how to do it for Wordpress anymore which is I have never handled Wordpress.

Please or to participate in this conversation.