** Tested with Laravel 7.x and WordPress 5.5
- Download the sql table "wp_users" from WordPress, and upload it to the laravel mysql table.
- Create the migration:
php artisan make:migration update_users_table_for_wp --table=users
- \database\migrations[DATE]_update_users_table_for_wp.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use App\User;
use Illuminate\Support\Facades\DB;
class UpdateUsersTableForWp extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('password')->nullable()->change();
});
// Migrate from WP Users > Laravel Users
DB::table('wp_users')
->orderBy('id')
->chunk(100, function ($wp_users) {
foreach ($wp_users as $wp_user) {
// add new user (if doesn't exist)
User::firstOrCreate(
[
'email' => $wp_user->user_email,
],
[
'name' => $wp_user->user_nicename,
'password' => $wp_user->user_pass
]
);
}
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('users', function (Blueprint $table) {
//
});
}
}
?>
- Migrate
php artisan migrate
- Install Laravel-wp-password package
composer require mikemclin/laravel-wp-password
- \app\Providers\EventServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Event;
use Illuminate\Auth\Events\Attempting;
use App\Listeners\WordPressPasswordUpdate;
class EventServiceProvider extends ServiceProvider
{
/**
* The event listener mappings for the application.
*
* @var array
*/
protected $listen = [
Attempting::class => [
WordPressPasswordUpdate::class,
],
Registered::class => [
SendEmailVerificationNotification::class,
],
];
/**
* Register any events for your application.
*
* @return void
*/
public function boot()
{
parent::boot();
//
}
}
- \app\Listeners\WordPressPasswordUpdate.php
<?php
namespace App\Listeners;
use MikeMcLin\WpPassword\Facades\WpPassword;
use Illuminate\Auth\Events\Attempting;
use Illuminate\Support\Facades\Hash;
class WordPressPasswordUpdate
{
public function handle(Attempting $event)
{
$this->check($event->credentials['password'], \App\User::where('email', $event->credentials['email'])->first()->password ?? 'not found');
}
public function check($value, $hashedValue, array $options = [])
{
if (Hash::needsRehash($hashedValue))
{
if (WpPassword::check($value, $hashedValue))
{
$newHashedValue = (new \Illuminate\Hashing\BcryptHasher)->make($value, $options);
\Illuminate\Support\Facades\DB::update('UPDATE users SET `password` = "' . $newHashedValue . '" WHERE `password` = "' . $hashedValue . '"');
$hashedValue = $newHashedValue;
}
}
}
}