WordPress is a common web content management platform that allows users to build, maintain, and publish web pages. Wix Headless allows you to integrate many of Wix's business solutions into your WordPress site.
This tutorial shows you how to create a WordPress plugin that integrates your Wix Headless project with your WordPress site. It includes implementing a "Buy Now" flow that allows your visitor to purchase a product from a list.
You can find the full source code here.
In this tutorial, you will:
First, set the Wix Headless project to act as your site's dashboard. Here you can install apps to add Wix functionality to your WordPress site.
To set up the Wix Headless project, follow these steps:
If you haven't already, create a Wix Headless project. When prompted to add functionalities to your new project, select eCommerce.
Set up authorization by creating and configuring an OAuth app on your Headless project. Name the app wix_example_client_id
. Note your client ID. You will need it later to integrate your WordPress site with the Wix Headless project.
Finally, make sure that the Wix Stores app is installed: In your Headless project dashboard, click Apps. If the Wix Stores app is not installed, install it. This Wix Stores app allows your WordPress site to access the Wix Stores API.
A WordPress plugin is a zip
file that contains one or more PHP files. These files contain the functionality you want to add to your site with some metadata. This step describes how to write a script that automatically creates a zip
file with your updated plugin files.
This script builds your code as a zip
file. The script is written in JavaScript in a node.js environment. However, you can use any build tool and runtime environment you prefer.
Create the plugin zip file:
Create a new folder named headless-wordpress-tutorial
. This folder will contain all the plugin files, including the build script and the finalized zip
plugin file.
In this folder, create a new file named wix-headless-example.php
. In it, add the following code:
This serves as the basis for the plugin's functionality.
In the same headless-wordpress-tutorial
folder, create two new folders:
build
, to contain the automated build script.plugin
, to contain the plugin's functionality.In build
, create a new file named zip-plugin.js
, and write the following code:
This code removes any existing zip
files, and generates a Wordpress-ready plugin.zip
file, with all your plugin's functionality, in the dist
folder. Whenever you add functionality to your plugin, you need to run the build script again, and upload the updated plugin.zip
file to your Wordpress site.
To test whether the plugin is recognized and rendered correctly on your WordPress site, include a test shortcode:
Update wix-headless-example.php
with the following code:
From the headless-wordpress-tutorial
root directory, type node build/zip-plugin.js
to run the build script.
On your site's WP Admin panel, click Plugins
-> Add New
-> Upload Plugin
, and upload dist/zip-plugin.js
to the site.
Activate the plugin.
Launch one of your site's pages and add a shortcode block with the content [initial_shortcode]
.
Save and view the page to see the result.
Once you've confirmed the plugin is rendered on your site, remove the code you added in Step 1.
Once you have installed the OAuth app on your Wix Headless project dashboard, your Headless project is configured to provide services only to clients that carry the correct client ID.
To enable communication between your WordPress site and your Headless project, configure your site with the correct client ID as provided by the OAuth app in Step 1. However, since the WP Admin panel does not have a built-in settings page to enable it to integrate with Wix Headless, you need to manually create a plugin settings page.
To create a plugin settings page and configure it with the client ID:
In the plugin
folder, create a new folder named templates
.
In plugin/templates
, create a new file named settings.php
with the following code:
This file creates the structure of the plugin's settings page.
Add the plugin's settings page to your site's Admin panel. In wix-headless-example.php
, add the following code:
This code creates, registers, and renders your plugin's settings page on your WordPress site's Admin panel.
Lastly, on your WordPress site's Admin panel, set the client ID as it appears on your Headless project's OAuth settings app:
On your WordPress site's Admin panel, click Settings > Wix Headless Example.
In the Wix Client ID input field, paste the client ID as it appears on your Headless project's Headless Settings page.
Your WordPress site is now configured as the valid client of your Wix Headless project.
To access Wix APIs, a client needs to carry valid access tokens. These include the client ID as defined in Step 2. Access tokens are only valid for several hours. When they expire, they must be refreshed. Without a valid authorization token, a client cannot make requests to a Headless project.
This step shows how to add a simple authorization service to your plugin. The service retrieves and checks for valid access tokens, refreshes them if necessary, and saves, or persists, them locally to your browser as cookies.
Note: For additional information about creating and using access tokens, see the Making API Calls with OAuth in the Wix REST API reference.
Important: To keep this tutorial simple and easy to understand, this authorization service applies to all devices. You might want to handle access tokens separately for each type of device.
To create the authorization service:
In your plugin
folder, create a new folder named services
. In it, create a new file named wix-auth.php
and add to it the following code:
This creates the basic structural outline of the authorization service.
Add the functionality to retrieve, or get, any tokens that might already be locally saved in the form of a browser cookie:
This function checks and decodes any tokens that already exist in a user's browser. If no cookies are found, the function returns null.
Add the functionality to persist tokens in the user's browser as cookies:
This function saves a token to a user's browser in the form of a cookie. Each token is valid for 30 days, and can be used to access all parts of the site.
Add the functionality to check whether an access token has expired:
Use the Wix API to get and refresh tokens:
This code defines two types of tokens, one for site visitors and one for logged-in site members. Then, the generateVisitorTokens()
function creates and processes visitor tokens using the Wix REST Token
endpoint.
These tokens are required to make calls to Wix APIs on behalf of your visitor. Without them, your calls will be not be authorized. You can read more about visitor tokens in the Wix Headless REST API documentation.
Finally, now that tokens have been set up, set the authorization service to initialize as soon as your WordPress site initializes the plugin.
Still in services/wix-auth.php
, add the service initialization function:
This function checks for existing visitor tokens, and if none are found, creates and persists new ones. It makes sure that your site has valid tokens when making calls to Wix APIs.
Back in wix-headless-example.php
, bring the authorization service into the scope of the file:
These use
and require_once
statements allow the functions in this file to access the functions defined in the authorization service.
Finally, register and call the authorization service's initialization method:
The add_filter()
function registers the init()
function for the init
WordPress hook. This means that, whenever the init
WordPress hook fires, the local init()
function runs and initializes the authorization service.
Whenever a Wix API responds to a client request, a unique request ID is included in the response. This can be helpful if you need to contact Wix support for help. In this step, you will add a service to retrieve the request ID as returned by the Wix API.
To get the Wix request ID:
In the plugin
folder, create a new folder named utils
. This folder will contain various utility services.
In plugin/utils
, create a new file named wix-request-id.php
and add to it the following code:
When called, this function extracts the Wix request ID from the given response, and returns it to the caller.
Back in services/wix-auth.php
, call the get_wix_request_id()
method whenever an error is logged by the generateVisitorTokens()
function:
Adding a call to get_wix_request_id()
whenever tokens have failed to generate makes it easier to debug, resolve errors, and request additional help from Wix's support teams.
Now that you can make authorized calls to Wix APIs, you can integrate any of Wix Headless's business solutions into your site. In this tutorial, you will implement two eCommerce functionalities—a product list, and a checkout.
Important: A complete eCommerce user flow is usually comprised of a product list, a single-product page, a cart, and a checkout. However, this tutorial only covers creating a product list and a checkout. This is because this tutorial focuses on a pure server-side implementation, while implementing a product page and a cart requires client-side code using the Wix eCommerce SDK.
The first Wix eCommerce functionality to implement is the product list. Use the Wix Stores API to create a service that queries and lists the products in your Wix Headless project's CMS.
First, create the service:
In the plugin/services
folder, create a new file named wix-products.php
.
In wix-products.php
, add the following code:
The getProducts()
function makes an authorized call to the Wix Stores API. It queries all products that exist in your Headless project's CMS.
Next, create a WordPress template file to display your product list:
In plugin/templates
, create a new file named product-list.php
.
In product-list.php
, add the following code:
This code creates the template for displaying a dynamic list of products.
Now that the products have been queried, and a template exists for displaying them, all that is left is to display them on your site.
In plugin/wix-headless-example.php
, add the following code:
This wix_headless_render_product_list()
function queries the existing products and uses the product-list
template file to render the products on the screen. It also registers this function for the wixwp_products
shortcode.
Note: The locate_template()
function allows the theme to override the existing template.
Finally, on your WordPress site, add a shortcode block with the [wixwp_products]
shortcode. View your page to see the list of products.
The second Wix eCommerce functionality you will implement is the Wix secured checkout. This involves implementing a dynamic "Buy Now" route for each product. When clicked, the route takes your visitor to a secured checkout page where they can complete their purchase.
Note: As its name suggests, the "Buy Now" functionality takes your visitor directly to a checkout page, without taking them through a product page and a shopping cart first. This means that product variants or options are not supported in this implementation. For a full eCommerce implementation, see the Wix Headless eCommerce tutorial.
To create a dynamic checkout page for each product, queried each product must first be identified with a unique, dynamic URL. In plugin/services/wix-products.php
, update the value of the request body to include a dynamic slug:
Now, when making a call to the Wix Stores API, the request body includes a unique URL slug for each product.
Wix Headless offers a secured checkout service that can be dynamically populated with each product's details.
Use the Wix eCommerce API to create a secured checkout service:
In plugin/services
, create a new file named wix-checkout.php
.
Add to it the following code:
The createCheckout()
function makes a call to the Wix eCommerce API with the details of the product that was clicked. The function returns a new checkout object used to render the Wix Checkout page on your WordPress site.
Now, whenever a product is clicked, a checkout page dynamically loads with that product's details. However, the checkout page itself is a Wix page, which means it isn't hosted on your WordPress site, but rather on Wix's servers. Therefore, your visitor still needs to be redirected there to complete their purchase, and back to your WordPress site once they are done.
To redirect your visitor from your WordPress site to the Wix checkout page, and then return them to your site once they have completed their purchase, you need to use the Wix Redirect Session API.
Create a redirection service to handle redirecting your visitor to, and back from, the Wix checkout page:
Still in plugin/services/wix-checkout.php
, add the following code:
The createCheckoutRedirectSession()
function makes an authorized call to the Wix Redirect Sessions API. The postFlowUrl
value included in the call specifies where your visitor returns upon completing their purchase. The function then returns the processed redirect session to the calling router.
The redirection functionality is now configured, but it is not integrated into your page. You now need to create a routing service that manages the entire "Buy Now" redirection and checkout flow.
Create the routing service:
In plugin/utils
, create a new file named routes.php
.
In routes.php
, write the following code:
The wix_headless_buy_now_endpoint()
specifies which Wix REST endpoint must be called whenever a product is clicked. Its callback function, wix_headless_buy_now_callback()
, creates both the dynamic checkout page with the clicked product's details, and the necessary redirection functionality.
The product list, checkout, and redirect services have been set up. You now need to let your main plugin logic know that they are available and where it can find them. Your WordPress site also needs to be aware of the Wix REST endpoints used by these services.
Bring the router into the scope of your plugin. In plugin/wix-headless-example.php
, add the following lines:
The use .../WixStoresProducts
statement makes the product list, checkout, and redirection functionalities available to the functions in the current file. The various require_once
statements act as safeguards to make sure that files already brought into scope won't be brought in again.
Finally, register the "Buy Now" endpoint to your WordPress site. In plugin/wix-headless-example.php
, add the following line:
The add_action()
WordPress function registers wix_headless_buy_now_endpoint()
as the callback function to be called whenever the rest_api_init
hook fires. Whenever your WordPress site initializes its REST API functionality, the wix_headless_buy_now_endpoint()
function runs and begins the "Buy Now" flow.
Finally, embed the "Buy Now" option in the list of products on your WordPress site. In templates/product-list.php
, add the following line to your code:
When a visitor clicks a product, they are redirected to the checkout page to complete their purchase, and redirected back to your site once they have completed their purchase.
Well done! In this tutorial you created from scratch a WordPress plugin that integrates a limited Wix Headless eCommerce user flow with your WordPress site. It was done using server-side code only.
You can find the full source code here.
However, You can expand the existing implementation to include other business services, such as product pages and a cart, by adding client-side code. For a sample implementation, see the Wix Headless eCommerce tutorial.