PHPackages                             contrainteractive/wp-laravel-login - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. contrainteractive/wp-laravel-login

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

contrainteractive/wp-laravel-login
==================================

Allows a user to login to a Laravel application using an existing WordPress hashed password.

v1.1.0(9mo ago)16[1 issues](https://github.com/ContraInteractive/wp-laravel-login/issues)MITPHP

Since Jan 16Pushed 9mo ago2 watchersCompare

[ Source](https://github.com/ContraInteractive/wp-laravel-login)[ Packagist](https://packagist.org/packages/contrainteractive/wp-laravel-login)[ RSS](/packages/contrainteractive-wp-laravel-login/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (2)Versions (3)Used By (0)

WP Laravel Login
----------------

[](#wp-laravel-login)

WP Laravel Login is a PHP library that enables seamless user authentication in a Laravel application using existing WordPress hashed passwords.

### Features

[](#features)

- Authenticate Laravel users with WordPress hashed passwords.
- Compatible with existing WordPress installations.
- Simplifies integration for WordPress and Laravel hybrid applications.

Installation
You can install the package via composer:

```
composer require contrainteractive/wp-laravel-login
```

### Usage

[](#usage)

#### There is multiple ways to use this plugin.

[](#there-is-multiple-ways-to-use-this-plugin)

1. **Migrate from WP to Laravel**:

    - This is the recommended way to use the package. It provides a custom user provider that validates user credentials against WordPress hashed passwords.
    - The package also includes an artisan command to copy WordPress users to Laravel.
    - Simply import your users from WP with their existing hashes and then users auth into your new Laravel Environment
2. **Wordpress/Laravel Hybrid**

    - This is where you might have Laravel and WP running side-by-side for a given time
    - In this scenario, still import your users into Laravel
    - When a user updates their password on Wordpress invoke the provided API sync route
    - See `Syncing Passwords` below

### Docs

[](#docs)

**WordPress User Authentication**

The package provides a custom `WpUserProvider` that validates credentials by:

1. Validates passwords against the WordPress hashing format.
2. Updates passwords to Laravel’s hashing mechanism upon successful login (unless disabled).

Update auth.php to use the custom provider:

```
'providers' => [
   'users' => [
      'driver' => 'wp',  // env('AUTH_MODEL', App\Models\User::class),
 ],
```

**Password Rehashing Behavior**

By default, the package updates the password hash to Laravel’s format after login. To disable this, update the config/wp-login.php file:

```
// config/wp-login.php
// Enable or disable the preservation of the WordPress hash after user login.
return [
 'preserve_wp_hash' => false,
];
```

- There isn't a known use case to me where this would ever be true, but you are welcome to use it.

### Copy WordPress Users to Laravel

[](#copy-wordpress-users-to-laravel)

Included is a basic artisan command to copy WordPress users to Laravel. This is useful for migrating users from WordPress to Laravel.

```
php artisan wp:copy-users --table-prefix=custom_wp --host=localhost --database=my_wp_db --username=admin --password=password123
```

You can also define a db connection in the `config/database.php` file instead of command arguments. Which is a more friendly way of using this package. The `wp:copy-users` command will automatically use it

```
  'wp' =>
	  [
            'driver'    => 'mysql',
            'host'      => env('WP_DB_HOST', '127.0.0.1'),
            'database'  => env('WP_DB_DATABASE', 'wordpress'),
            'username'  => env('WP_DB_USERNAME', 'root'),
            'password'  => env('WP_DB_PASSWORD', ''),
            'charset'   => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix'    => env('WP_DB_PREFIX', 'wp_'),
	  ],
```

Fetch and copy WordPress users programmatically:

```
$users = DB::connection('wp')
 ->table('wp_users')
 ->select('ID', 'user_login', 'user_pass', 'user_email', 'user_registered')
 ->get();

// You might then Copy WordPress users to Laravel
// if you do insert()/save() a user into Laravel.  Know that larvel likes to auto-hash
// So Compare your WP DB against your Larvel DB to make sure the passwords match after initial import

foreach ($wpUsers as $wpUser) {
    try {
        DB::table('users')->updateOrInsert(
			['email' => $wpUser->user_email],
			[
				'name'       => $wpUser->user_login,
				'email'      => $wpUser->user_email,
				'password'   => $wpUser->user_pass,// retain wp hash
				'created_at' => $wpUser->user_registered,
				'updated_at' => now(),
			]
		);

		 $this->info("Copied user: {$wpUser->user_login}");
	 } catch (\Exception $e) {
		 $this->error("Failed to copy user {$wpUser->user_login}: " . $e->getMessage());
	 }
 }
```

You can also just create a wordpress user as a test with this :

```
