There are many aspects of your WordPress site that could benefit from a pinch of artificial intelligence. We could, for instance, use natural language processing techniques to parse the content of your post and automatically tag it, suggest related posts for better linking or predict its popularity. Or, analyze the comments with sentiment analysis algorithms to quickly understand whether the post is generating positive or negative reactions among your readers. Or many other things.

But, surprisingly, very few of these ideas have actually been implemented. Just to give you an example, right now, the official WordPress repository only shows two plugins tagged with the keyword tensorflow. My guess is that many people think that integrating Machine Learning / Deep Learning or, in general, any Artificial Intelligence technique in WordPress is very difficult.

But it’s not. Applying state-of-the-art ML techniques on WordPress is easier than you think. First, because there are more and more simple Machine Learning tools for non-coders or that abstract most of the ML complexity so that you can create flexible ML solutions with very little code. And more importantly, because for many of the typical applications you can think of, there are already pretrained and ready to use ML models you could just import and directly use in your WordPress plugins / sites.

True, the result of importing and tuning a trained model may not be as good as if you had trained the model yourself with your data and context but in many scenarios the user would not feel the difference and you’d be able to make your WordPress smarter without having to go through the complete machine learning process of training and testing an adhoc model from scratch. For instance, AutoML has plenty of models to play with but all major players are also releasing more and more models of their own.

Once you found the model, embedding it into WordPress is typically rather easy. The exact process will depend on how the model is provided and the framework you use. As an example, I’m showing you how I did it in my Serious Toxic Comments plugin. The goal of this plugin is to flag and block toxic comments (racist comments, obscenities, insults,…) from your site. It turns out that Tensorflow, and more specifically, TensorFlow.js, provides a toxicity classifier. Given a text string and a threshold (to tune the sensitivity of the classifier) you get as a result the classification of the text as healthy or toxic (and if so, it indicates the type of toxicity).

Feel free to check the Toxic Comments plugin source code on GitHub. Most of the code is boilerplate code (btw, mostly generated thanks to my WordPress DSL) but the key code snippet that calls tensorflow to analyze the comment is the following:

I basically intercept the submit event from the comment form, take the comment text, classify it and then, depending on the result, go ahead with the submission or ask the user to rewrite the comment. The classification process uses the global variable toxicity exposed by the tensorflow model, loads the model and uses it to classify input. We wait for the results and then check whether the comment has been flagged in any of the toxic categories supported by the model.

And that’s it!. With less than 20 lines of javascript we’ve been able to create a plugin that uses advanced natural language and machine learning techniques to improve the user experience in our WordPress site!

Share This