Skip to main content

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' β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜