How to add AI to your WordPress site

Ken Burcham
6 min readAug 10, 2018

--

You’ve heard about Artificial Intelligence in everything from airplanes to toasters and you’ve wondered how to get those benefits into your WordPress website. Well, wonder no more! I’ve created a WordPress plugin that integrates with Algorithmia that is easy to extend to any AI algorithm they provide! FYI: Algorithmia does have many platform integrations this post discusses just the WordPress one.

Algorithmia is an algorithm platform with an API you can use to run intelligent algorithms with sometimes astonishing Machine Learning complexity from your own code or website. They provide client libraries for all of the major programming languages (the PHP client written by yours truly!), making it easy to add AI algorithms directly into your own applications. And after I finished the PHP client, the next logical step was to build a WordPress plugin to bring the power of AI to the more than 25% of the internet powered by WordPress!

Full disclosure: I am an occasional contractor for Algorithmia, sometimes writing my own algorithms/code for my own use and sometimes for pay. It has been an awesome way for me to get into AI programming!

The Algorithmia-WP plugin

The WordPress plugin gives you a few algorithms you can use right away on your website. It is meant to be a template so that you can easily add ANY of the thousands of algorithms available in Algorithmia, but these are the ones you can use out of the box:

  • Nudity filtering for uploads — when a picture is uploaded to your website, enable a deep learning nudity detection algorithm at Algorithmia to either block the picture or warn the uploader. (This is the same algorithm that powers https://isitnude.com)
  • SEO image auto-tagging — when a picture is uploaded to your website, an object recognition algorithm will return labels that will be added to the alt text of the image.
  • Post summarizer/abstract generator — when a new post is created on your website, this plugin uses a Machine Learning algorithm to summarize your post into a few sentences and adds the summary to the top of the post. Not really something you’ll probably want to use in production, but a fun demo nonetheless!

If you want to get started right away, you can clone the WordPress plugin from GitHub or add the plugin directly from WordPress.org. Then follow the instructions to create an account and get an API key from Algorithmia, activate the plugin and then use those algorithms on your site immediately.

But, you might say to yourself, Algorithmia supports a vast number of algorithms in their marketplace and new ones are being added every day… So what if you want to measure the emotional sentiment of comments and get an email if someone posts something extremely negative? Or what if you want to colorize images that are uploaded your site?

Below I will explain how to add the ability to measure the emotional sentiment (positive or negative) of a comment posted to your site using natural language processing, text analysis and computational linguistics to identify and extract the feeling of what the poster wrote. It will even translate from other languages if it can detect it!

This is a task that Machine Learning is quite good at but is quite complicated to implement and train. Fortunately for us, we can leverage the work done by the folks at Stanford and simply call their ML platform and algorithm hosted at Algorithmia.

Add a new algorithm (comment sentiment analysis) to the plugin

For following along the rest of the article, I’m assuming you’re comfortable writing PHP and have some experience with WordPress.

To get started, you’ll need to have a working WordPress site. Then follow the instructions here to clone or download and extract the Algorithmia-WP plugin into your wp-content/plugins folder. You can also install the plugin from the WordPress.org plugins repository. Then open up your favorite editor (I’m using Visual Code) and open up the plugins folder:

Ready to add an AI algorithm to our plugin!

The objective: a sentiment tag added to the comment

What I want our new plugin to do is send the algorithm the content of a newly posted comment and analyze it. If the comment text gets a positive score, I want to add: “sentiment: positive” to the bottom of the comment. If it is a negative score, I want to add “sentiment: negative (warning!)”. If it turns out to be exactly zero, it should say “sentiment: neutral”.

Step 1: Lookup the algorithm input/output

The algo that we’re going to add is found here: https://algorithmia.com/algorithms/nlp/SentimentAnalysis

Take a look at the input the algorithm expects and the output it produces:

Note it wants a “document” property on a JSON object and will return an array with the first element as an object with a property “sentiment” that’ll have the positive or negative value. The value will be a sentiment score from -1 to +1 with zero being completely neutral. Our objective, then, is to populate a JSON object with a “document” property that is the text of our incoming comment. Then read the response for the “sentiment” property and add in the appropriate text to the comment at the right place.

Step 2: Add the algorithm file and get started!

Under the “algorithms” folder, create a new file called “analyze_comment_sentiment.php”. Once this file is created, the code in the main plugin file (“algorithmia_plugin.php”) will automagically load it since it loads all PHP files it finds in the algorithms directory when it starts up. If you’ve activated the Algorithmia plugin in your WordPress website then this file will be loaded on the next request.

Add a new file for our plugin

TIP: Take a look at the post_summarizer.php file as the simplest example of calling a text-based algorithm. The others (upload_auto_tag and upload_nudity_detect) are a bit more complex, but will get you started working with binary files with algorithms.

Step 3: Lookup the WordPress filter / hook we need to use

Unless you already happen to know which WordPress filter/hook you need, you’ll have to google around to figure it out. I searched for “wordpress filter before insert comment” and found this page: https://codex.wordpress.org/Plugin_API/Filter_Reference/preprocess_comment which is just what we need.

Step 4: Do the codez

In our new plugin file, let’s add in the filter and a function that WordPress will call every time a new comment is added:

<?php
/* Use Algorithmia AI to analyze the sentiment of new comments*/
//before a comment is saved:
add_filter( 'preprocess_comment' , 'comment_analyze_sentiment' );
function comment_analyze_sentiment( $commentdata ) { //do magic return $commentdata;
}

Now when our function gets called, the $commentdata will be an array populated with our comment info and the actual text of the comment (according to the documentation noted above) will be in $commentdata['comment_content'].

So let’s wrap it with a JSON object, setup the client with the API key (you need to set that in the settings according to the instructions! don’t forget!) and then prepare the call to our algorithm:

function comment_analyze_sentiment( $commentdata ) {    //do magic

$input = array( "document" => $commentdata['comment_content'] );
$apikey = get_option('algo_options')['algo_field_api'];
$client = Algorithmia::client($apikey);
//send our post content to Algorithmia's AI for processing!
$algo = $client->algo("nlp/SentimentAnalysis");
$result = $algo->pipe($input)->result;
$sentiment = $result[0]->sentiment; //process the sentiment return $commentdata;
}

Now the only remaining thing to do is process the sentiment with an if/ifelse. Here’s the final code in my editor:

This tiny bit of code will add full-blown neural net sentiment analysis to your website!

Now if you pop over to your WordPress site and add a couple of comments, you’ll have auto-magic sentiment analysis being added, courtesy of Deep Learning Artificial Intelligence!!

Conclusion

This tutorial walks you through adding AI to your WordPress website using Algorithmia, the full-blown PHP client and Algorithmia-WP plugin. Now you can browse the Marketplace and find another couple of plugins and integrate them yourself and bring Artificial Intelligence into your WordPress website!

About the author:

Ken Burcham does freelance web engineering from rural Eastern Oregon. He’s worked in the field of Software Engineering for most of his life and loves riding motorcycles and having family adventures (like living in Cambodia for 3 years!). He can be contacted at kenburcham@gmail.com or http://github.com/kenburcham

--

--