Installation Guide
Prerequisitesβ
WP-Node requires access to a WordPress-compatible MySQL database. The easiest way to set this up is with Docker.
If you donβt already have a WordPress database running, you can use the following command to spin up both MariaDB and WordPress containers:
docker network create wpnet && \
docker run -d --name wpdb --network wpnet -p 33306:3306 \
-e MYSQL_ROOT_PASSWORD=example \
-e MYSQL_DATABASE=wordpress \
-e MYSQL_USER=wp \
-e MYSQL_PASSWORD=wp \
mariadb && \
docker run -d --name wp --network wpnet -p 8080:80 \
-e WORDPRESS_DB_HOST=wpdb:3306 \
-e WORDPRESS_DB_USER=wp \
-e WORDPRESS_DB_PASSWORD=wp \
-e WORDPRESS_DB_NAME=wordpress \
wordpress
Visit http://localhost:8080 in your browser to complete the WordPress setup.
π‘ For this tutorial, use
wp
as your WordPress admin username.
Helpful Tipsβ
View WordPress logs:
docker logs -f wp
Clean up all Docker containers and network:
docker rm -f wp wpdb && docker network rm wpnet
Alternative Database Setupβ
You can use the WP-Node CLI to install a WordPress database without installing WordPress. For detailed instructions, see Install CLI.
Initialize WP-Node Projectβ
To get started, create a new folder for your project. This folder will serve as the root directory for your WP-Node application.
mkdir wp-node
cd wp-node
Then, run the command to initialize the project and follow the prompts:
npx @rnaga/wp-node-cli -- init
β Enter your database hostname: Β· localhost
β Enter your database port: Β· 33306
β Enter your database username: Β· wp
β Enter your database password: Β· **
β Enter your database name: Β· wordpress
β Is it a multi-site? Β· No
β Enter your static assets path: Β· public
Project Structureβ
After initialization, your project will look like this:
./
βββ _wp
βΒ Β βββ config
βΒ Β βΒ Β βββ index.d.ts
βΒ Β βΒ Β βββ wp.json
βΒ Β βββ settings.ts
βββ .env
βββ index.ts
βββ package-lock.json
βββ package.json
βββ tsconfig.json
Key files
_wp/config/wp.json
: Holds configuration for WP-Node such as public path and multisite info. This file is imported by settings.ts._wp/settings.ts
: Initializes the WP-Node Context, including config, database access and hooks.index.ts
: The main entry point for your WP-Node app. A basic sample is provided..env
: Stores sensitive environment variables, including your database credentials and other configuration values required at runtime.
Run the Appβ
Once the config is initialized, run the app using:
mvn use 22
npx ts-node ./index.ts
If everything is working correctly, youβll see SQL output like:
select * from `wp_posts` as `posts_5` where `posts_5`.`ID` = 1
[
{
ID: 1,
post_author: 1,
post_title: 'Hello world!',
...
}
]
Use Cli (Alternative)β
WP-Node CLI provides a convenient way to interact with WordPress data without writing any code.
To query a post (e.g. ID = 1), run:
npx @rnaga/wp-node-cli -- post get 1 -Z table -F ID,post_title,post_type
ββββββββββββββ¬βββββββββββββββββ
β (index) β Values β
ββββββββββββββΌβββββββββββββββββ€
β ID β 1 β
β post_title β 'Hello world!' β
β post_type β 'post' β
ββββββββββββββ΄βββββββββββββββββ