Episode 043: Before you learn JS deeply, learn PHP and WordPress

Aired: 01/17/2017

PHP is the language that WordPress was built on, and has been powering web applications for nearly a decade, which is ancient in Internet years. JavaScript, the newest big buzz language that has been moving around the WordPress community for the past couple years, and has spawned some awesome projects, tutorials, and has shifted focus to the WordPress REST API and what you can do with it (now in core).

However, the WordPress REST API is not built in JavaScript, it is built in PHP. PHP is the language that all of WordPress is built on, so being able to hack a theme by inserting the_title(); in somewhere isn’t going to cut it much anymore if you want to move into advanced JS development, powered by the REST API.

Business Logic will be PHP

If you are using the WordPress REST API to power a JavaScript application, most of the main business logic will stay in PHP. You will make a call to the API, the PHP will handle the request and do business logic (i.e add to cart, payments, CPT manipulation) and return a response which your JS will then use to modify the view in some way. I think people have made the assumption (and I have built) applications that put a lot more of the business logic in JavaScript.

In my case, I’d make a few calls to the API to gather data, do business logic, and send something to the API to save in the database. I quickly realized that this is not the way to do it, let PHP and the server handle all of the business logic, even dealing with other API’s, and only use JS as a UI layer. It can be a very robust UI layer which calls on the WordPress REST API for data, or other API’s for data, but no actual business logic.

WordPress won’t be Node

We don’t foresee a near future where WordPress is built on node, at least not the .org version. Maybe if the stack for PHP did not evolve, but it has come leaps and bounds, making it easy nowadays to create and run on a great server that powers PHP applications. To build WordPress out again, powered by a node stack would just be a rebuild, and also would mean its not going to be as easy to configure, manipulate, etc.

You don’t need JS to use the API

One of the other misconceptions we tackled was that you need JavaScript to use the WordPress REST API, which is false. Josh even mentioned a great use case where loading with PHP, using the same functionality (the REST Request) you can preload data for your JavaScript application before it even loads client side, making first load even faster for the user.

Bringing back the topic of hosting, I mention that a few years ago I used to spread the gospel of Client Side Applications. Why load on the server, when you can load client side and leave the server open for more concurrent users. However with great advances in how WordPress is hosted, and scalable cloud solutions this is no longer the case. Using PHP to load first load, but making a reactive, event-driven UI layer in JavaScript is probably the hybrid solution that (for right now) is going to be the best bang for the buck.

PHP Deeply

Leave a Reply

Your email address will not be published. Required fields are marked *

Please enter an e-mail address

This site uses Akismet to reduce spam. Learn how your comment data is processed.