• Stars
    star
    112
  • Rank 312,240 (Top 7 %)
  • Language
    PHP
  • License
    MIT License
  • Created about 8 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Provides means to sync Wordpress posts to your Laravel DB in order to use Laravel as the front-end to your WP blog (cos lets face it - WP templates suck ass)

WordpressToLaravel

Latest Version on Packagist Software License

You want a blog, right? And you want to use the Wordpress backend? But you dislike Wordpress themes, and REALLY dislike creating them? Then this is for you my friend...

This package provides the tool-set to sync WP posts from the WP DB and into your Laravel DB (or wherever you want it to go). This way you can still use the WP backend to author your posts and manage the backend users etc, but gives you glorious freedom to custom code your own frontend, without the nonsense that is WP themes.

It exposes itself as an Artisan command, which means you can set it up to run on a schedule (using Laravel's scheduler) to sync your WP posts, categories, tags and authors to your Laravel DB.

On to installation...

Install

Via Composer:

$ composer require leeovery/wordpress-to-laravel

After adding the package, add the ServiceProvider to the providers array in config/app.php:

LeeOvery\WordpressToLaravel\WordpressToLaravelServiceProvider::class,

Publish the config file:

$ php artisan vendor:publish --provider="LeeOvery\WordpressToLaravel\WordpressToLaravelServiceProvider" --tag="config"

Migrate your database to setup the posts, categories, tags & author tables:

$ php artisan migrate

Setup the url to your WP blog in your env file:

WP_TO_LARAVEL_API_URL=https://blog.your-blog-domain.com/

Finally, we need to configure WP itself. If you're using Wordpress 4.7+, then you're all set - crack on! Otherwise, you'll need to install the WP API plugin to the WP site you wish to sync from:

Wordpress API

Usage

Firstly, it's best to perform a full sync to get all your posts etc across in one go. After this it'll only sync page 1 of modified posts (by default).

To force sync all published posts:

$ php artisan wordpress-to-laravel:import -F -T

The -F flag forces all posts to be synced. The -T flag will truncate all the relevant DB tables prior to syncing.

You can rerun that at any time to truncate and resync all your posts etc.

Once that's done, you should setup the following in your Laravel app so that all recently modified WP posts are synced across to the local DB:

$schedule->command('wordpress-to-laravel:import')
                 ->everyMinute();

Showing Posts

Syncing is only half the job. You'll want to show your posts etc on your blog. But that's super easy too.

Just use the Post, Category & Author Eloquent models that are supplied. Alternatively you can provide your own if you need extra methods or functionality.

For ease I suggest extending from the supplied models. If you want to use your own you should update the config file with your models. Also make sure the transformers work with your model(s) too, otherwise you'll need to supply new versions of those too (they should extend the AbstractTransformer from Fractal), and update the appropriate config value for that models transformer.

Example usage of supplied models (this code would appear in your BlogController, BlogTagController & BlogCategoryController):

// to show newest 5 posts, paginated...
$posts = Post::orderBy('published_at', 'desc')
                     ->paginate(5);

// to fetch a post using the slug, or fail...
$post = Post::slug($slug)->firstOrFail();

// to fetch tag by tag slug, or fail...
$tag = Post::createTagsModel()->whereSlug($tag)->firstOrFail();

// to fetch newest 5 posts (paginated) by tag slug (from above)...
$posts = Post::whereTag($tag->slug)
             ->orderBy('published_at', 'desc')
             ->paginate(5);

// to fetch category by category slug, or fail...
$category = Category::whereSlug($category)->firstOrFail();

// to fetch newest 5 posts (paginated) by category slug (from above)...
$posts = Post::whereCategory($category->slug)
             ->orderBy('published_at', 'desc')
             ->paginate(5);

Media

When you upload media using the WP backend the links will point to your WP blog url. This might be ok for you. But for us it wasn't. So we installed the S3 plugin on our WP site. This means that all media uploaded via WP will be posted up to your S3 storage, and the URLs to said images will be rewritten on the fly to the S3 location. These updated media urls will be respected when you sync.

Redirecting WP Frontend To New Frontend

Lets say your WP blog is at https://blog.example.com, and your new Laravel frontend is at https://example.com/blog/. Ideally, you'll want your old frontend to redirect to your new frontend. This is simple to achieve by creating a little empty theme and activating it in your WP backend.

  1. Create new theme in your WP themes dir called 'redirection_theme'

  2. Create a file called style.css, and insert the following:

    /*
    Theme Name: turn off frontend
    Theme URI:
    Description:
    Author:
    Version:
    License: GNU
    License URI:
    Tags:
    */
  3. Create a file called index.php, and insert the following:

    <?php
    global $wp;
    $url = str_replace('blog.', '', home_url() . '/blog/' . $wp->request);
    wp_redirect($url, 301);
    die();
  4. Finally, create a file called functions.php, and insert the following:

    <?php
    function redirection_theme_change_view_link($permalink)
    {
        $url = str_replace('blog.', '', env('WP_HOME') . '/blog');
    
        return str_replace(env('WP_HOME'), $url, $permalink);
    }
    add_filter('post_link', 'redirection_theme_change_view_link');
  5. Don't forget to activate it in your WP backend.

Enjoy!

Contributing

Please see CONTRIBUTING and CONDUCT for details.

Credits

License

The MIT License (MIT). Please see License File for more information.