The following will help you setup Next.js WordPress.
- Requirements
- Quick Start
- Manual Setup (Next.js)
- Manual Setup (WordPress)
- Custom Post Types and Fields
- Up Next
- Clone this repo:
npx degit gregrickaby/nextjs-wordpress nextjs-wordpress
- Set up your dev environment:
npm run setup
Copy .env.sample
to .env.local
cp .env.sample .env.local
That's it for Next.js. You're ready to move on to the WordPress setup.
All WordPress commands must be run from inside the /wordpress
directory. Docker Desktop needs to be running as well.
This step can be done at a later time. Open the /apps/wordpress/.env
file in your code editor.
From inside the /wordpress
directory, run the following command:
chmod +x install.sh && ./install.sh
This script pulls down and starts each container. Composer will download the required plugins, WordPress will be configured, and finally the script add an entry to your host file.
Note: You will be prompted to enter your password so the script can edit your
/etc/hosts
file
View the WordPress dashboard at: https://nextjswp.test/wp-admin/
- username
admin
- password
password
Use the credentials above, unless you've customized the
WORDPRESS_USERNAME
andWORDPRESS_PASSWORD
variables in the/apps/wordpress/.env
file.
In order for Next.js to POST
comment and reaction data back to WordPress, it needs to authenticate using an application password.
- Navigate to your Profile page in the WordPress dashboard under
Users --> Profile
- Scroll down to the Application Password section
- Type any name to the New Application Password Name field
- Click the "Add New Application Password" button:
- Copy the password that was generated and paste into
.env
: - Open the
.env
file in the root of this project - Paste the Application Password next to the
WORDPRESS_APPLICATION_PASSWORD
key:
In order for post reactions to work, you'll need to import the ACF fields.
- Navigate to the WordPress dashboard under
Custom Fields --> Tools
- Under "Import Field Groups" click the "Choose File" button and select the
acf-export-post-fields.json
file from the/wordpress
directory: - Click the "Import JSON" button
That's it! WordPress should now be setup and ready to go.
The front-end has support for a "Books" custom post type with custom fields. This is just a simple example, so feel free to edit or remove as needed.
-
Visit the CPT UI tools page: https://nextjswp.test/wp-admin/admin.php?page=cptui_tools
-
Copy and paste the following JSON into the "Import Post Types" field:
{ "book": { "name": "book", "label": "Books", "singular_label": "Book", "description": "", "public": "true", "publicly_queryable": "true", "show_ui": "true", "show_in_nav_menus": "true", "delete_with_user": "false", "show_in_rest": "true", "rest_base": "", "rest_controller_class": "", "rest_namespace": "", "has_archive": "false", "has_archive_string": "", "exclude_from_search": "false", "capability_type": "page", "hierarchical": "false", "can_export": "false", "rewrite": "true", "rewrite_slug": "", "rewrite_withfront": "true", "query_var": "true", "query_var_slug": "", "menu_position": "", "show_in_menu": "true", "show_in_menu_string": "", "menu_icon": "dashicons-book-alt", "register_meta_box_cb": null, "supports": ["title", "editor", "excerpt", "thumbnail", "custom-fields"], "taxonomies": [], "labels": { "menu_name": "My Books", "all_items": "All Books", "add_new": "Add new", "add_new_item": "Add new Book", "edit_item": "Edit Book", "new_item": "New Book", "view_item": "View Book", "view_items": "View Books", "search_items": "Search Books", "not_found": "No Books found", "not_found_in_trash": "No Books found in trash", "parent": "Parent Book:", "featured_image": "Featured image for this Book", "set_featured_image": "Set featured image for this Book", "remove_featured_image": "Remove featured image for this Book", "use_featured_image": "Use as featured image for this Book", "archives": "Book archives", "insert_into_item": "Insert into Book", "uploaded_to_this_item": "Upload to this Book", "filter_items_list": "Filter Books list", "items_list_navigation": "Books list navigation", "items_list": "Books list", "attributes": "Books attributes", "name_admin_bar": "Book", "item_published": "Book published", "item_published_privately": "Book published privately.", "item_reverted_to_draft": "Book reverted to draft.", "item_scheduled": "Book scheduled", "item_updated": "Book updated.", "parent_item_colon": "Parent Book:" }, "custom_supports": "", "enter_title_here": "", "show_in_graphql": "1", "graphql_single_name": "Book", "graphql_plural_name": "Books" } }
-
Click the "Import" button.
- Visit the ACF tools page: https://nextjswp.test/wp-admin/edit.php?post_type=acf-field-group&page=acf-tools
- Under the Import settings, click "choose file" and select
/apps/wordpress/acf-export-cpt-fields.json
- Click the blue "Import" button
Learn more about Local Development