Code Monkey home page Code Monkey logo

wordpress-to-laravel's Introduction

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

If you are using wordpress.com to host your blog, set the following env variable to true (default is false). This is done because wordpress.com and wordpress.org (self hosted instances) have different url structure when fetching the posts

WP_TO_LARAVEL_IS_WORDPRESS_COM=true

Next, add your WP blog url to the env file

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

or for wordpress.com hosted blogs (change the your-blog part of the url)

WP_TO_LARAVEL_API_URL=https://public-api.wordpress.com/wp/v2/sites/your-blog.wordpress.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.

wordpress-to-laravel's People

Contributors

leeovery avatar ruibeard avatar serdarcevher avatar vesper8 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

wordpress-to-laravel's Issues

Will not install Laravel 5.5.4

Just tried to run on a fresh install but get requirements could not be resolved to an installable set of packages

Detailed description

This is my terminal output

$ composer require leeovery/wordpress-to-laravel
Using version ^1.2 for leeovery/wordpress-to-laravel
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

Problem 1
- Conclusion: remove laravel/framework v5.5.4
- Conclusion: don't install laravel/framework v5.5.4
- leeovery/wordpress-to-laravel v1.2.0 requires illuminate/events 5.3.|5.4. -> satisfiable by illuminate/events[v5.3.0, v5.3.16, v5.3.23, v5.3.4, v5.4.0, v5.4.13, v5.4.17, v5.4.19, v5.4.27, v5.4.36, v5.4.9].
- leeovery/wordpress-to-laravel v1.2.1 requires illuminate/events 5.3.|5.4. -> satisfiable by illuminate/events[v5.3.0, v5.3.16, v5.3.23, v5.3.4, v5.4.0, v5.4.13, v5.4.17, v5.4.19, v5.4.27, v5.4.36, v5.4.9].
- leeovery/wordpress-to-laravel v1.2.2 requires illuminate/events 5.3.|5.4. -> satisfiable by illuminate/events[v5.3.0, v5.3.16, v5.3.23, v5.3.4, v5.4.0, v5.4.13, v5.4.17, v5.4.19, v5.4.27, v5.4.36, v5.4.9].
- leeovery/wordpress-to-laravel v1.2.3 requires illuminate/events 5.3.|5.4. -> satisfiable by illuminate/events[v5.3.0, v5.3.16, v5.3.23, v5.3.4, v5.4.0, v5.4.13, v5.4.17, v5.4.19, v5.4.27, v5.4.36, v5.4.9].
- don't install illuminate/events v5.3.0|don't install laravel/framework v5.5.4
- don't install illuminate/events v5.3.16|don't install laravel/framework v5.5.4
- don't install illuminate/events v5.3.23|don't install laravel/framework v5.5.4
- don't install illuminate/events v5.3.4|don't install laravel/framework v5.5.4
- don't install illuminate/events v5.4.0|don't install laravel/framework v5.5.4
- don't install illuminate/events v5.4.13|don't install laravel/framework v5.5.4
- don't install illuminate/events v5.4.17|don't install laravel/framework v5.5.4
- don't install illuminate/events v5.4.19|don't install laravel/framework v5.5.4
- don't install illuminate/events v5.4.27|don't install laravel/framework v5.5.4
- don't install illuminate/events v5.4.36|don't install laravel/framework v5.5.4
- don't install illuminate/events v5.4.9|don't install laravel/framework v5.5.4
- Installation request for laravel/framework (locked at v5.5.4, required as 5.5.*) -> satisfiable by laravel/framework[v5.5.4].
- Installation request for leeovery/wordpress-to-laravel ^1.2 -> satisfiable by leeovery/wordpress-to-laravel[v1.2.0, v1.2.1, v1.2.2, v1.2.3].

Installation failed, reverting ./composer.json to its original content.

Your environment

XAMPP
PHP 7.1.9
Laravel 5.5.4

Not compatible with Laravel 5.5

$ composer require leeovery/wordpress-to-laravel

Composer throw this error ;

Problem 1
- Conclusion: remove laravel/framework v5.5.22
- Conclusion: don't install laravel/framework v5.5.22

TypeError argument passed must be type array null given

Faced this issue while running composer require leeovery/wordpress-to-laravel on last step when it says
Generateing Optimized autoload files

Detailed description

TypeError
Argument 3 passed to LeeOvery\WordpressToLaravel\WordpressToLaravel::__construct() must be of the type array, null given, call
ed in C:....\vendor\leeovery\wordpress-to-laravel\src\WordpressToLaravelServiceProvider.php on line 74

at C:....\vendor\leeovery\wordpress-to-laravel\src\WordpressToLaravel.php:80
76▕ * @param FractalManager $fractalManager
77▕ * @param GuzzleClient $client
78▕ * @param array $config
79▕ */
➜ 80▕ public function __construct(FractalManager $fractalManager, GuzzleClient $client, array $config)
81▕ {
82▕ $this->fractalManager = $fractalManager;
83▕ $this->client = $client;
84▕ $this->config = $config;

It's a bug.

Context

I am trying to build a front website for my WordPress blog.

Your environment

  • php 7.4
  • windows 10
    *Localhost not published
  • Laravel 8.27 & 8.44

Page load time.

This is more a query rather than issue.

Does this allow page load speed increase with the use of Laravel for the frontend. (i.e. you can developer better cache with Laravel's built in caching).

-F/--force-all doesn't actually exist in the code?

Finally getting on to sorting a proper import, etc and have noticed that there isn't actually any mention of --force-all in the code? Only page, per-page and truncate exist in Importer.php while --force-all is only mentioned in the signature.

Undefined property: stdClass::$_embedded

[Edit2] Might as well use this for a real issue now.

This is in PostTransformer.php

It seems the REST API v2 doesn't report back anything called $_embedded relating to the featured image of a post. Am I missing something here?

i got php 7

Could not find package leeovery/wordpress-to-laravel at any version matching your PHP version 5.6.30.0

cant install

Category not sync after Update Category in Blog

I found the issue , when I try to sync my website with the blog. that's work like charm.

butw when i already to edit category name in blog , and run sync command (php artisan wordpress-to-laravel:import).

note : category name , not the category of post.

the category not updated.

i use laravel 5,3 and php ver 5 .

*sorry for bad english.

I'm having trouble getting the cron job to run

It works perfectly through the CLI. I can get it to run using artisan schedule:run and using artisan wordpress-to-laravel:import.

However, it doesn't run on my scheduler cron job. So then I setup a specific cron job for artisan wordpress-to-laravel:import to see if I can get it to run that way and also get the error emailed to me.

I'm running Laravel 5.4 on PHP 7.1.1

Thanks for any pointers!

Here's the error it throws:

[Symfony\Component\Console\Exception\CommandNotFoundException]          
 There are no commands defined in the "wordpress-to-laravel" namespace

[2017-07-10 21:50:01] local.ERROR: Symfony\Component\Console\Exception\CommandNotFoundException: There are no commands defined in the "wordpress-to-laravel" namespace. in /home/general/public_html/project/vendor/symfony/console/Application.php:499
Stack trace:
#0 /home/general/public_html/project/vendor/symfony/console/Application.php(531): Symfony\Component\Console\Application->findNamespace('wordpress-to-la...')
#1 /home/general/public_html/project/vendor/symfony/console/Application.php(186): Symfony\Component\Console\Application->find('wordpress-to-la...')
#2 /home/general/public_html/project/vendor/symfony/console/Application.php(120): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#3 /home/general/public_html/project/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(123): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#4 /home/general/public_html/project/artisan(34): Illuminate\Foundation\Console\Kernel->handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#5 {main} 
```.  

Your requirements could not be resolved to an installable set of packages.

composer create-project --prefer-dist laravel/laravel larawpt2
cd larawpt2
raj@raj-K45A:/var/www/html/larawpt2$ composer require leeovery/wordpress-to-laravel
Using version ^2.1 for leeovery/wordpress-to-laravel
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

Problem 1
- Installation request for leeovery/wordpress-to-laravel ^2.1 -> satisfiable by leeovery/wordpress-to-laravel[v2.1].
- Conclusion: remove laravel/framework v5.7.2
- Conclusion: don't install laravel/framework v5.7.2
- leeovery/wordpress-to-laravel v2.1 requires illuminate/events 5.6.* -> satisfiable by illuminate/events[5.6.x-dev, v5.6.0, v5.6.1, v5.6.10, v5.6.11, v5.6.12, v5.6.13, v5.6.14, v5.6.15, v5.6.16, v5.6.17, v5.6.19, v5.6.2, v5.6.20, v5.6.21, v5.6.22, v5.6.23, v5.6.24, v5.6.25, v5.6.26, v5.6.27, v5.6.28, v5.6.29, v5.6.3, v5.6.30, v5.6.31, v5.6.32, v5.6.33, v5.6.34, v5.6.35, v5.6.36, v5.6.37, v5.6.38, v5.6.4, v5.6.5, v5.6.6, v5.6.7, v5.6.8, v5.6.9].
- don't install illuminate/events 5.6.x-dev|don't install laravel/framework v5.7.2
- don't install illuminate/events v5.6.0|don't install laravel/framework v5.7.2
- don't install illuminate/events v5.6.1|don't install laravel/framework v5.7.2
- don't install illuminate/events v5.6.10|don't install laravel/framework v5.7.2
- don't install illuminate/events v5.6.11|don't install laravel/framework v5.7.2
- don't install illuminate/events v5.6.12|don't install laravel/framework v5.7.2
- don't install illuminate/events v5.6.13|don't install laravel/framework v5.7.2
- don't install illuminate/events v5.6.14|don't install laravel/framework v5.7.2
- don't install illuminate/events v5.6.15|don't install laravel/framework v5.7.2
- don't install illuminate/events v5.6.16|don't install laravel/framework v5.7.2
- don't install illuminate/events v5.6.17|don't install laravel/framework v5.7.2
- don't install illuminate/events v5.6.19|don't install laravel/framework v5.7.2
- don't install illuminate/events v5.6.2|don't install laravel/framework v5.7.2
- don't install illuminate/events v5.6.20|don't install laravel/framework v5.7.2
- don't install illuminate/events v5.6.21|don't install laravel/framework v5.7.2
- don't install illuminate/events v5.6.22|don't install laravel/framework v5.7.2
- don't install illuminate/events v5.6.23|don't install laravel/framework v5.7.2
- don't install illuminate/events v5.6.24|don't install laravel/framework v5.7.2
- don't install illuminate/events v5.6.25|don't install laravel/framework v5.7.2
- don't install illuminate/events v5.6.26|don't install laravel/framework v5.7.2
- don't install illuminate/events v5.6.27|don't install laravel/framework v5.7.2
- don't install illuminate/events v5.6.28|don't install laravel/framework v5.7.2
- don't install illuminate/events v5.6.29|don't install laravel/framework v5.7.2
- don't install illuminate/events v5.6.3|don't install laravel/framework v5.7.2
- don't install illuminate/events v5.6.30|don't install laravel/framework v5.7.2
- don't install illuminate/events v5.6.31|don't install laravel/framework v5.7.2
- don't install illuminate/events v5.6.32|don't install laravel/framework v5.7.2
- don't install illuminate/events v5.6.33|don't install laravel/framework v5.7.2
- don't install illuminate/events v5.6.34|don't install laravel/framework v5.7.2
- don't install illuminate/events v5.6.35|don't install laravel/framework v5.7.2
- don't install illuminate/events v5.6.36|don't install laravel/framework v5.7.2
- don't install illuminate/events v5.6.37|don't install laravel/framework v5.7.2
- don't install illuminate/events v5.6.38|don't install laravel/framework v5.7.2
- don't install illuminate/events v5.6.4|don't install laravel/framework v5.7.2
- don't install illuminate/events v5.6.5|don't install laravel/framework v5.7.2
- don't install illuminate/events v5.6.6|don't install laravel/framework v5.7.2
- don't install illuminate/events v5.6.7|don't install laravel/framework v5.7.2
- don't install illuminate/events v5.6.8|don't install laravel/framework v5.7.2
- don't install illuminate/events v5.6.9|don't install laravel/framework v5.7.2
- Installation request for laravel/framework (locked at v5.7.2, required as 5.7.*) -> satisfiable by laravel/framework[v5.7.2].

Installation failed, reverting ./composer.json to its original content.

Cannot Used on laravel 11

Cannot used on Laravel 11

Detailed description

i want to use it with laravel 11 but it cannot installed

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.