Skip to content

Instantly share code, notes, and snippets.

@Yiannistaos
Last active September 3, 2020 12:06
Show Gist options
  • Save Yiannistaos/b32db7af83b5ea7c9e981d027690b336 to your computer and use it in GitHub Desktop.
Save Yiannistaos/b32db7af83b5ea7c9e981d027690b336 to your computer and use it in GitHub Desktop.
Migrating WordPress users and passwords to Laravel (tested with Laravel 7.x and WordPress 5.5)

How to Migrating WordPress users and passwords to Laravel

** Tested with Laravel 7.x and WordPress 5.5

STEPS


  1. Download the sql table "wp_users" from WordPress, and upload it to the laravel mysql table.
  2. Create the migration:
php artisan make:migration update_users_table_for_wp --table=users
  1. \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) {
                //
            });
        }
    }
    ?>
  1. Migrate
php artisan migrate
  1. Install Laravel-wp-password package
composer require mikemclin/laravel-wp-password
  1. \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();

        //
    }
}
  1. \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;
            }
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment