Building a Recommendation System with ChatGPT PHP in a Custom WordPress Plugin

Posted on 17th June 2023

In this article, we’ll be building a recommendation system with ChatGPT PHP in a custom WordPress plugin. This plugin will be responsible for two things:

  • Integrating with the ChatGPT API to fetch recommendations
  • Rendering the recommendations in the WordPress admin backend

Integrating with the ChatGPT API

The first thing we need to do is sign up for a ChatGPT account. Once you have an account, you’ll be able to create an API key.

With your API key, you can then make calls to the ChatGPT API. The API has a number of methods, but the one we’re interested in is the getRecommendations method.

This method takes two arguments:

  • userId: The id of the user for whom you want to fetch recommendations
  • numRecommendations: The number of recommendations to fetch

The getRecommendations method returns a JSON object with a recommendations property. This property is an array of recommendation objects, each of which has the following properties:

  • id: The id of the recommended item
  • title: The title of the recommended item
  • url: The url of the recommended item
  • imageUrl: The url of the recommended item’s image

Now that we know how the API works, let’s move on to building our plugin.

Building the Plugin

Our plugin will be a simple PHP class that lives in the /wp-content/plugins directory. The plugin will have the following methods:

  • __construct: The plugin’s constructor method. This method will be responsible for setting up the plugin.
  • fetchRecommendations: This method will be responsible for making the API call to fetch recommendations.
  • renderRecommendations: This method will be responsible for rendering the recommendations in the WordPress admin backend.

Let’s take a look at each of these methods in more detail.

The __construct Method

The __construct method is the plugin’s constructor method. This method is responsible for setting up the plugin.

In our __construct method, we need to do two things:

  • Register the plugin’s settings
  • Add a callback for the wp_ajax_fetch_recommendations action

Let’s take a look at how to do each of these things.

Registering the Plugin’s Settings

The first thing we need to do in our __construct method is register the plugin’s settings. We’ll be using the WordPress register_setting function to do this.

The register_setting function takes three arguments:

  • $option_group: The name of the option group to which the setting belongs
  • $option_name: The name of the setting
  • $sanitize_callback: (optional) A callback function that sanitizes the setting’s value

In our case, we’ll be registering two settings:

  • chatgpt_api_key: The API key for our ChatGPT account
  • chatgpt_user_id: The id of the user for whom we want to fetch recommendations

We’ll also need to provide a $sanitize_callback function for both settings. This callback function will be responsible for sanitizing the setting’s value.

The WordPress sanitize_text_field function is a good choice for sanitizing both setting values. This function ensures that the setting values are sanitized before they’re stored in the database.

Here’s how our __construct method would look with the settings registered:

public function __construct() {
  register_setting( 'chatgpt_settings', 'chatgpt_api_key', 'sanitize_text_field' );
  register_setting( 'chatgpt_settings', 'chatgpt_user_id', 'sanitize_text_field' );
}

Adding a Callback for the wp_ajax_fetch_recommendations Action

The second thing we need to do in our __construct method is add a callback for the wp_ajax_fetch_recommendations action.

This action is fired when an AJAX request is made to the /wp-admin/admin-ajax.php file with the action parameter set to fetch_recommendations.

In our callback function, we need to do two things:

  • Make the API call to fetch recommendations
  • Return the recommendations as a JSON object

Here’s how our callback function would look:

public function fetch_recommendations_callback() {
  // Make the API call to fetch recommendations
  $recommendations = $this->fetchRecommendations();
  
  // Return the recommendations as a JSON object
  wp_send_json( $recommendations );
}

The fetchRecommendations Method

The fetchRecommendations method is responsible for making the API call to fetch recommendations.

In this method, we need to do three things:

  • Get the API key and user id from the plugin’s settings
  • Make the API call to fetch recommendations
  • Return the recommendations

Let’s take a look at how to do each of these things.

Getting the API Key and User Id from the Plugin’s Settings

The first thing we need to do in our fetchRecommendations method is get the API key and user id from the plugin’s settings.

We can use the WordPress get_option function to get these values from the database.

The get_option function takes two arguments:

  • $option: The name of the option to retrieve
  • $default: (optional) The default value to return if the option does not exist

In our case, we’ll be passing in the chatgpt_api_key and chatgpt_user_id settings as the $option argument.

We’ll also need to provide a default value for each setting in case the setting does not exist in the database. A good default value for the chatgpt_api_key setting would be an empty string, and a good default value for the chatgpt_user_id setting would be 1.

Here’s how our fetchRecommendations method would look with the API key and user id retrieved from the plugin’s settings:

public function fetchRecommendations() {
// Get the API key and user id from the plugin's settings
$apiKey = get_option( 'chatgpt_api_key', '' );
$userId = get_option( 'chatgpt_user_id', 1 );

// Make the API call to fetch