WordPress Playground: a real-world use for PHP in WebAssembly

WordPress Playground: a real-world use for PHP in WebAssembly
A fix

The WordPress team has developed Playground, which runs the ubiquitous CMS (Content Management System) in the browser or in Node.js, with the potential for simplifying the development of plugins and themes.

The project is based on WebAssembly (Wasm) PHP, including WebAssembly SQLite for the database, and networking within the browser using an emscripten project called Aysyncify, which pauses PHP while JavaScript executes.

“When PHP makes a network request, it yields control back to JavaScript, which makes the request, and then resumes PHP when the response is ready. It works well enough that PHP build can request web APIs, install composer packages, and even connect to a MySQL server,” the docs explain.

The two primary scenarios currently are running WordPress in the browser, which is useful for trying it out, and running WordPress in Node.js, which enables a working, temporary WordPress installation for local development using Visual Studio code. 

Editing a WordPress site that exists only in the browser

“This could be used for web development, for trying out a plugin before you install it. I think we’re just at the beginning of understanding what this means. Maybe it could be built into WordPress to test out things. If you just click a button, it clones your existing wordpress, downloads it to your browser, and then you can make any changes you want, when you’re done, deploy it back to your live website,” said Matt Mullenweg when the project was previewed at the State of the Word WordPress event late last year.

The WordPress team is not the first to run PHP WebAssembly, and in fact its implementation is based on Sean Morris’s php-wasm. VMWare has also ported PHP to WebAssembly. It is notable though that the Playground team has some clarity about the purpose of its work. “WordPress Playground exists to make WordPress instantly accessible for users, learners, extenders, and contributors by building foundational software tools developers can use to create interactive, zero-setup, JavaScript applications with WordPress,” states the documentation.

Another project enthused by the possibilities of PHP in WebAssembly is Wasmer, which has its own implementation called wasmer-php. Wasmer CEO Syrus Akbary argues that modern cloud infrastructure based on containers and Kubernetes is too complex and expensive for most applications. “All this complexity deepens your dependency on the cloud, it ties you to the big tech providers,” he said last month, introducing an early preview of Wasmer Edge. PHP compiled to Wasm means there is a prospect of porting server applications to this kind of lightweight infrastructure.

CloudFlare workers are also a possible deployment target, though when the WordPress Playground team tried this they ran into issues with excessive memory consumption.

Seeing something akin to the veteran LAMP (Linux, Apache, MySQL, PHP) stack running entirely in the browser is remarkable; but a counter-argument is that it could perpetuate technology from which developers should move on. “The PHP/Wordpress model where pages are composited dynamically for each pageview was an idea that made sense 20+ years ago. Whereas today, regenerating 1000 (or 10,000) static HTML pages can be done in a few seconds,” remarked a developer on Hacker News, commenting on the Playground. It may be true; but according to some statistics 43 percent of all websites run WordPress so a project that improves the developer experience for building themes and plugins will be of substantial interest.