SLIM PHP, TWIG and WordPress

Recently I was asked to develop a website to promote a new service a client was planning to offer. My plan was to use SLIM PHP and TWIG as the initial requirements for the site were minimal with a limited number of pages needed and no admin to manage content.

Pretty basic. So SLIM PHP Micro Framework seemed like a great choice since all I really wanted from a framework was to manage URIs and requests. The bigger PHP frameworks like Laravel and Symfony bring a ton of features to the table that were just not needed. So I went micro with SLIM. With selecting the backbone complete, I wanted a quick solution for managing templating the website. The choice for me was simple, TWIG. I used TWIG on a few Symfony projects and liked how flexible the engine is.

With the project well underway, the requirements began to evolve and more options were needed than the initial project request. I should have seen this coming but the client did insist that he wanted something basic without the extra work of managing content on a daily basis. That changed and development had begun with a bulk of the foundation in place. With other projects mounting and no time to rethink and execute a new approach, I needed to build on my initial approach.

Use WordPress to Manage Content & Admin

My solution was to integrate WordPress with SLIM PHP and TWIG. WordPress would handle the content, forms, and admin. I adjusted my routing in SLIM to bind to WordPress pages and posts using WP_QUERY. All external Javascript and CSS files used by WordPress plugins are now isolated to only the page using the plugin limiting the number of calls needed to load a page which is great for SEO.

Below is a sample of my integration of WordPress to my SLIM PHP application. WordPress is installed in a subdirectory within the SLIM application root. I disabled WP Cron for improved performance by adding define('DISABLE_WP_CRON', 'true'); to wp-config.php.

In the SLIM App's index.php file, I added the following

I created a route file to hold my WordPress post type integration. This keeps your project organized. This can be repeated for page types as well as custom post types.

Have a question, ask it here

This post was written by Steven Soehl Tampa based web developer & designer, online marketer, and WordPress.

Tags

Posted Under: , ,

Leave a Reply

7 Comments on "SLIM PHP, TWIG and WordPress"

Notify of
avatar
Sort by:   newest | oldest | most voted
Charlie
Guest

Hi Steven,
Wouldn’t it be simpler to use the WordPress REST API rather than fully loading WP in your Slim App?

TofZed
Guest

Cockpit-CMS as API/headless CMS would be even more simpler than using a bulldozer like WordPress

Nathan Robjohn
Member

Hey man, did comment on your reddit start but no reply. I am just wondering why you used both slim and WordPress? Yes I know you developed it initially in slim then was asked for more functionality but I really don’t I understand why you wouldn’t just use your html and css and convert it into wordpress after all it’s only now copying and pasting functions etc? I mean now you have slim and WordPress both running for no reason?

TofZed
Guest

“With the project well underway, the requirements began to evolve and more options were needed than the initial project request” => It’s so common.
90% of my web projects started with this sentence from the customer: “this will be a very simple website with just a few pages”
And in reality the scope is really really different. Like a hundred of pages, or with several languages, using post-types, lots of custom fields, lots of page templates, etc…
And of course the real work load is at minimum 3 times more than what defined on initial scope…

hire-laravel-developer
Guest

Nice Blog! Thanks for sharing very useful post. keep it up.

wpDiscuz