Blogs

Laravel 5 and WordPress Integration

Laravel 5 and WordPress Integration

Wordpress


WordPress is web software you can use to create a beautiful website or blog. We like to say that WordPress is both free and priceless at the same time.

Themes can be easily downloaded from the official WordPress site or from hundreds of other places around the web. The same goes with plugins, which are used to extend the functionality of your WordPress site.
In addition to being a fantastic blog and content management system, one of the huge benefits is the wealth of information out there. There’s a great community of people behind the design & development of the WordPress system itself. People from all over the world contribute their time, knowledge and skill to keeping WordPress updated and secure.

Laravel 5


Laravel is a free, open-source PHP web framework, created by Taylor Otwell and intended for the development of web applications following the model–view–controller (MVC) architectural pattern. Some of the features of Laravel are a modular packaging system with a dedicated dependency manager, different ways for accessing relational databases, utilities that aid in application deployment and maintenance, and its orientation toward syntactic sugar.

Why Laravel & Wordpress integration?

There are times when building a site is advantageous as it prevents you from having to retrain the user on how to manage content, menus, photos, and more. A popular way of setting up a site like this is to use WordPress as the admin and then build out the front end in a framework such as Laravel.

How to integrate Laravel & Wordpress?

Let's install Laravel first using Composer. This will take a while.

composer create-project laravel/laravel


So we have the Laravel folder structure with everything installed. Now let's install a fresh installation of Wordpress inside Laravel. Here you have two options:

1. Install Wordpress as a sub-directory of public Laravel folder, like /public/wordpress. To access your Wordpress Admin you have to go to something like http://example.com/wordpress/wp-admin.

2. Install Wordpress as a sub-directory of the Laravel's root, like /wordpress. So you will have /app and /wordpress in the same position. For this you have to create another VirtualHost that points to Wordpress installation. You can setup a subdomain like wp.example.com and point it to /laravel/folder/wordpress. This way you can access the Admin going to http://wp.example.com/wp-admin.

Removing the Wordpress front-end

For security issues go to /wordpress/index.php file and put this first, to redirect to the Admin login page:

header("Location: ./wp-admin");
exit();

So, when you visit http://wp.example.com or http://example.com/wordpress you are redirected to the Wordpress Admin login page.

Connect Laravel to Wordpress

Now we have to include Wordpress inside Laravel. Just edit the public/index.php Laravel file and include the following before anything:

/*
| Integrate Wordpress with Laravel core
|
*/
define('WP_USE_THEMES', false);
require __DIR__.'/../wordpress/wp-blog-header.php';

Now you can use Wordpress functions with Laravel.

Querying Posts

Let's suppose you need to get some posts that display the view file:

// app/controllers/SchoolController.php
class SchoolController extends BaseController
{
public function index()
{
$query = new WP_Query(array(
'post_type' => 'school',
'posts_per_page' => 20,
'order' => ASC,
'orderby' => 'post_title',
));
$posts = $query->get_posts();

return View::make('school.index')->with('schools', $posts);
}
}

Topics: WordPress Laravel

e-Zest is a leading digital innovation partner for enterprises and technology companies that utilizes emerging technologies for creating engaging customers experiences. Being a customer-focused and technology-driven company, it always helps clients in crafting holistic business value for their software development efforts. It offers software development and consulting services for cloud computing, enterprise mobility, big data and analytics, user experience and digital commerce.