|
| 1 | +# WordPress to Next.js Sitemap Integration |
| 2 | +## Overview |
| 3 | +A Next.js application that fetches and transforms WordPress sitemaps with clean URL formatting, providing a seamless integration between WordPress content and Next.js frontend. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +* XML Sitemap Transformation - Fetches WordPress sitemaps and transforms URLs to match your frontend |
| 8 | +* Clean URL Structure - Presents sitemap links with clean, user-friendly URLs |
| 9 | +* XSL Styling Support - Custom XSL stylesheets for better presentation of sitemap data |
| 10 | +* Sitemap Index Support - Handles both individual sitemaps and sitemap index files |
| 11 | +* SEO Friendly - Maintains all SEO benefits of XML sitemaps while using your frontend domain |
| 12 | + |
| 13 | +## Prerequisites |
| 14 | + |
| 15 | +* Node.js (v18+ recommended) |
| 16 | +* Next.js project |
| 17 | +* WordPress site with XML sitemaps enabled (core functionality or via plugin) |
| 18 | + |
| 19 | +## Setup |
| 20 | + |
| 21 | +### Environment Variables |
| 22 | + |
| 23 | +Create or update your .env.local file with: |
| 24 | +``` |
| 25 | +NEXT_PUBLIC_WORDPRESS_URL=http://your-wordpress-site.com |
| 26 | +NEXT_PUBLIC_FRONTEND_URL=http://your-nextjs-site.com |
| 27 | +``` |
| 28 | + |
| 29 | +Create API Route |
| 30 | + |
| 31 | +Create a file at `pages/sitemap.xml.js` with the code provided in this repository. |
| 32 | + |
| 33 | +## Add XSL Stylesheet |
| 34 | + |
| 35 | +Place the `sitemap.xsl` file in your public directory. |
| 36 | + |
| 37 | +## How It Works |
| 38 | +This integration: |
| 39 | + |
| 40 | +* Intercepts requests to `/sitemap.xml` on your Next.js site |
| 41 | +* Fetches the corresponding sitemap from your WordPress site |
| 42 | +* Transforms all URLs from WordPress domain to your frontend domain |
| 43 | +* Applies custom XSL styling for better presentation |
| 44 | +* Returns the transformed XML to the client |
| 45 | + |
| 46 | +For sitemap index files, it also: |
| 47 | + |
| 48 | +* Creates clean, friendly URLs for each child sitemap |
| 49 | +* Maintains proper linking through custom attributes |
| 50 | + |
| 51 | +## API Routes examples |
| 52 | + |
| 53 | +`/sitemap.xml` - Main sitemap endpoint that fetches and transforms WordPress sitemaps |
| 54 | +`/sitemap.xml?sitemap=/wp-sitemap-posts-post-1.xml` - Direct access to specific WordPress sitemaps |
| 55 | + |
| 56 | +# Running the example with wp-env |
| 57 | + |
| 58 | +## Prerequisites |
| 59 | + |
| 60 | +**Note** Please make sure you have all prerequisites installed as mentioned above and Docker running (`docker ps`) |
| 61 | + |
| 62 | +## Setup Repository and Packages |
| 63 | + |
| 64 | +- Clone the repo `git clone https://github.com/wpengine/hwptoolkit.git` |
| 65 | +- Install packages `cd hwptoolkit && pnpm install` |
| 66 | +- Setup a .env file under `examples/next/hybrid-sitemap-apollo/example-app` with `NEXT_PUBLIC_WORDPRESS_URL=http://localhost:8888` |
| 67 | +e.g. |
| 68 | + |
| 69 | +```bash |
| 70 | +echo "NEXT_PUBLIC_WORDPRESS_URL=http://localhost:8888" > examples/next/hybrid-sitemap-apollo/example-app/.env |
| 71 | +``` |
| 72 | + |
| 73 | +## Build and start the application |
| 74 | + |
| 75 | +- `cd examples/next/hybrid-sitemap-apollo` |
| 76 | +- Then run `pnpm example:build` will build and start your application. |
| 77 | +- This does the following: |
| 78 | + - Unzips `wp-env/uploads.zip` to `wp-env/uploads` which is mapped to the wp-content/uploads directory for the Docker container. |
| 79 | + - Starts up [wp-env](https://developer.wordpress.org/block-editor/getting-started/devenv/get-started-with-wp-env/) |
| 80 | + - Imports the database from [wp-env/db/database.sql](wp-env/db/database.sql) |
| 81 | + - Install Next.js dependencies for `example-app` |
| 82 | + - Runs the Next.js dev script |
| 83 | + |
| 84 | +Congratulations, WordPress should now be fully set up. |
| 85 | + |
| 86 | +| Frontend | Admin | |
| 87 | +|----------|------------------------------| |
| 88 | +| [http://localhost:3000/](http://localhost:3000/) | [http://localhost:8888/wp-admin/](http://localhost:8888/wp-admin/) | |
| 89 | + |
| 90 | + |
| 91 | +> **Note:** The login details for the admin is username "admin" and password "password" |
| 92 | +
|
| 93 | + |
| 94 | +## Command Reference |
| 95 | + |
| 96 | +| Command | Description | |
| 97 | +|------------------------|-----------------------------------------------------------------------------| |
| 98 | +| `example:build` | Prepares the environment by unzipping images, starting WordPress, importing the database, and starting the application. | |
| 99 | +| `example:dev` | Runs the Next.js development server. | |
| 100 | +| `example:dev:install` | Installs the required Next.js packages. | |
| 101 | +| `example:start` | Starts WordPress and the Next.js development server. | |
| 102 | +| `example:stop` | Stops the WordPress environment. | |
| 103 | +| `example:prune` | Rebuilds and restarts the application by destroying and recreating the WordPress environment. | |
| 104 | +| `wp:start` | Starts the WordPress environment. | |
| 105 | +| `wp:stop` | Stops the WordPress environment. | |
| 106 | +| `wp:destroy` | Completely removes the WordPress environment. | |
| 107 | +| `wp:db:query` | Executes a database query within the WordPress environment. | |
| 108 | +| `wp:db:export` | Exports the WordPress database to `wp-env/db/database.sql`. | |
| 109 | +| `wp:db:import` | Imports the WordPress database from `wp-env/db/database.sql`. | |
| 110 | +| `wp:images:unzip` | Extracts the WordPress uploads directory. | |
| 111 | +| `wp:images:zip` | Compresses the WordPress uploads directory. | |
| 112 | + |
| 113 | + |
| 114 | +>**Note** You can run `pnpm wp-env` and use any other wp-env command. You can also see <https://www.npmjs.com/package/@wordpress/env> for more details on how to use or configure `wp-env`. |
| 115 | +
|
| 116 | +### Database access |
| 117 | + |
| 118 | +If you need database access add the following to your wp-env `"phpmyadminPort": 11111,` (where port 11111 is not allocated). |
| 119 | + |
| 120 | +You can check if a port is free by running `lsof -i :11111` |
0 commit comments