🎥 Live demo: wpmovies.dev
Interactivity.API.-.WP.Movies.demo.mp4
This is a demo plugin which shows the features of the Interactivity API.
The plugin is split into:
-
/src/blocks/interactive
- Start here to understand how to build interactive blocks with the Interactivity API. The folder contains all the custom interactive blocks used in the demo. -
/wp-movies-theme
- The custom theme used in the demo. Contains some custom styling and the templates for the header & footer as well as the movie & actors pages. -
/lib
- Helpers and support code for this demo. It's not relevant for understanding the Interactivity API.
The Interactivity API is available in WordPress 6.5.
-
Install the required plugins:
-
If you use
wp-env
, runnpx wp-env start
to install the plugins automatically and run a local WP instance. -
Otherwise, install the following plugins:
-
Movies Demo Plugin (this repo)
-
-
Install the dependencies and build the plugin:
npm install && composer install npm run build
If you plan on tinkering with the frontend code, start the webpack server which automatically rebuilds the files when you make any changes:
npm start
-
Install & activate the theme:
You need to install and activate the Movies Demo Theme (
/wp-movies-theme
in this repo). If you are usingwp-env
, it is already installed by default and you just have to activate it. You can run:npx wp-env run cli "wp theme activate wp-movies-theme"
Note
When running wp-env
commands like the one above, it is assumed that the
Docker container is called cli
on your local machine. If you had
previously used wp-env
to run other WordPress sites, this name might be
different, e.g. cli-1
in which case the full command would be npx wp-env run cli-1 "wp theme activate wp-movies-theme"
.
-
Add the movie and actor data to the WordPress database:
You can import the data manually:
- Go to Tools > Import > WordPress and click on Run Importer.
- Select the
wp_sampledata_movies.xml
file. - Select the
Download and import file attachments
and click on theUpload file and import
. - Repeat the process for the
wp_sampledata_actors.xml
file. - Repeat the process for the
wp_sampledata_media.xml
file. This one can take up to five minutes.
If you are using
wp-env
, you can run the following commands:npx wp-env run cli "import wp-content/plugins/wp-movies-demo/wp_sampledata_movies.xml --authors=create" npx wp-env run cli "import wp-content/plugins/wp-movies-demo/wp_sampledata_media.xml --authors=create" npx wp-env run cli "import wp-content/plugins/wp-movies-demo/wp_sampledata_actors.xml --authors=create"
If you run into any problems you can run
npx wp-env clean all
and start this step over again. -
Set the permalinks to use the
Post name
in Settings > Permalinks.If you are using
wp-env
you can just run this command:npx wp-env run cli "wp rewrite structure '/%postname%/'"
-
Enable the Full client-side navigation in the Gutenberg > Experiments.
When enabled, the lists of movies and actors will paginate without doing a full page refresh. You can enable this behavior in your WordPress admin page in Gutenberg > Experiments. Click around to the next/previous page of the movies or actors. You the list is loaded without a delay. This is because the HTML for that page is prefetched ahead of time, and only the nodes that are different between the current page and the next page are updated.
When you add a movie to favourites notice how the number of movie likes is preserved when navigating to another page. In addition to client-side navigations, the Interactivity API uses a smart DOM diffing algorithm. This allows the interactive state of blocks on the current page to be preserved!
Try searching for movies or actors. The search results are rendered dynamically on the server!
Try opening the site editor and removing the "Search" template. You'll notice that the Search experience keeps working but that now the Search results look different. That's because in the absence of the Search template, the Archive template is being used. The Interactivity API is designed to work with the server-rendered markup and Full-site editing.
When you navigate to the page for a movie, you can play its trailer. If you have the client-side navigations enabled, you'll notice that the trailer will keep playing as you keep navigating around the site!