Implementing AI-based Text Summarization with ChatGPT PHP in a Custom WordPress Plugin

Posted on 19th June 2023

Introduction

In this tutorial, we will be implementing an AI-based text summarization technique called ChatGPT in a custom WordPress plugin. ChatGPT is a state-of-the-art text summarization technique that can be used to generate concise summaries of long texts. We will be using the transformers library to implement ChatGPT in our plugin. The plugin will take a long text as input, generate a summary of the text using ChatGPT, and output the summary in a WordPress post. The plugin will also allow the user to specify the desired length of the summary. This tutorial assumes that the reader has some experience with WordPress plugin development.

Setting up the Environment

Before we begin, we need to set up our development environment. We will be using WordPress version 5.4.2 and Composer version 1.10.5. We will also need to install the transformers library. Please follow the instructions in the link to install the transformers library. Once the environment is set up, we can begin developing our plugin.

Creating the Plugin

We will start by creating a new directory for our plugin. We will name our plugin chatgpt-text-summarizer. In this directory, we will create a file named chatgpt-text-summarizer.php. This file will contain the plugin’s main PHP code. We will also create a directory named includes. This directory will contain the plugin’s helper PHP files. We will name our main PHP file chatgpt-text-summarizer.php and our helper PHP file functions.php. The directory structure of our plugin should look like this:

chatgpt-text-summarizer/
├── chatgpt-text-summarizer.php
└── includes
    └── functions.php

The first thing we need to do is to add the following plugin header to chatgpt-text-summarizer.php:

<?php
/*
Plugin Name: ChatGPT Text Summarizer
Plugin URI: https://example.com/
Description: A plugin that uses ChatGPT to generate summaries of long texts.
Version: 1.0.0
Author: John Doe
Author URI: https://example.com/
License: GPLv2 or later
Text Domain: chatgpt-text-summarizer
Domain Path: /languages
*/

// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
	die;
}

In the header, we have specified the plugin’s name, description, version, author, and license. We have also specified the plugin’s text domain and domain path. The text domain is used for translation purposes. The domain path is the directory where the plugin’s translation files are stored. We have also added a conditional statement to abort if the file is called directly. This is a security measure to prevent direct access to the plugin’s PHP files.

Loading the Plugin’s Text Domain

Next, we need to add a function for loading the plugin’s text domain. We will name this function chatgpt_text_summarizer_load_textdomain(). This function will be added to our functions.php file. The function will take two arguments, the plugin’s text domain and the plugin’s absolute path. The function will use the load_plugin_textdomain() function to load the plugin’s text domain. The code for this function is as follows:

function chatgpt_text_summarizer_load_textdomain() {
    load_plugin_textdomain( 'chatgpt-text-summarizer', false, plugin_basename( dirname( __FILE__ ) ) . '/languages' );
}
add_action( 'plugins_loaded', 'chatgpt_text_summarizer_load_textdomain' );

In the code, we have first defined the chatgpt_text_summarizer_load_textdomain() function. This function uses the load_plugin_textdomain() function to load the plugin’s text domain. We have then added an action hook to call the chatgpt_text_summarizer_load_textdomain() function when the plugins_loaded action is fired. The plugins_loaded action is fired after all the plugins are loaded.

Defining the Plugin’s Settings

Next, we need to define the plugin’s settings. We will do this by creating an array of settings. We will name this array $chatgpt_text_summarizer_settings. This array will contain the following settings:

  • chatgpt_text_summarizer_setting_name: This setting will be used to store the name of the person who will be using the plugin. The default value for this setting is Anonymous.
  • chatgpt_text_summarizer_setting_email: This setting will be used to store the email address of the person who will be using the plugin. The default value for this setting is anonymous@example.com.
  • chatgpt_text_summarizer_setting_text: This setting will be used to store the text that will be summarized by the plugin. The default value for this setting is an empty string.
  • chatgpt_text_summarizer_setting_length: This setting will be used to store the desired length of the summary. The default value for this setting is 5.

The code for the $chatgpt_text_summarizer_settings array is as follows:

$chatgpt_text_summarizer_settings = array(
    'chatgpt_text_summarizer_setting_name'  => 'Anonymous',
    'chatgpt_text_summarizer_setting_email' => 'anonymous@example.com',
    'chatgpt_text_summarizer_setting_text'  => '',
    'chatgpt_text_summarizer_setting_length' => 5,
);

Registering the Plugin’s Settings

Next, we need to register the plugin’s settings. We will do this by using the register_setting() function. This function takes three arguments, the name of the option to be registered, the name of the group to which the option belongs, and an array of args. The args array can contain the following arguments: