How to Run Python Scripts in WordPress

Yes, you can run Python scripts in WordPress. You can either host the script on your server and use exec() or host it in the cloud and use wp_remote_get().

Published Categorized as WordPress

Did you know that you can run Python scripts in WordPress?

WordPress is a PHP application, and all PHP applications allow you to run Python scripts by using PHP’s native exec() command. As long as, of course, your web host hasn’t disabled this command. But we’ll get to that, and what you can do about it, in a moment.

But here’s the thing: even if your host has disabled this command — and they absolutely won’t let you turn it on under any condition — there’s still a way to run Python scripts in WordPress if you host them somewhere else and leverage the wp_remote_get() method.

Read on, and by the time you’re done with this tutorial, you’ll know exactly how to do this.

Create a Custom Plugin

You can run Python code in WordPress by creating a custom plugin for the job. The plugin will use the exec() command, which lets you execute external scripts within a PHP application.

The exec() command is supported by PHP 4, 5, 6, 7, and 8. However, it can be disabled in your server’s “php.ini” file through the disable_functions directive. Before creating a custom plugin for your WordPress website, check if your web host hasn’t disabled the exec() by default. And in case that they have, determine if you can enable it.

If your server’s running a compatible version of PHP and the exec() command isn’t disabled, continue with the steps below.

How to create the custom plugin:

  1. Fire up your FTP client, log on to your website’s FTP server, and go to the “/wp-content/plugins/” directory of your WordPress installation.
  2. Create a new folder called “my-custom-python-plugin” inside the “/wp-content/plugins/” directory.
  3. Next, create a new file called “plugin.php” inside the “my-custom-plugin” folder.
  4. Now create a new folder called “python-scripts” inside the “my-custom-plugin” folder. Upload your Python script — or Python scripts, in case you want to run more than one script — inside it.
  5. Open the “plugin.php” file for editing, then copy and paste the source code below inside it.

The source code to add to the “plugin.php” file:

<?php
/*
Plugin Name: My Custom Python Plugin
Description: This plugin lets you run Python scripts in WordPress.
Version: 1.0
*/

function run_python_script($script_name) {
    $output = exec('python ' . plugin_dir_path(__FILE__) . 'python-scripts/' . $script_name);
    return $output;
}
?>

This plugin contains a function that allows you to call the exec() command on any Python script uploaded to the “/wp-content/plugin/my-custom-python-plugin/python-scripts/” folder.

Let’s take a look at an example. Suppose I have a script called “example.py”. I’ve already went through the steps above, so all I have to do is call the function, store its return value in a variable, and do something with it, like on lines 13 and 14 below:

<?php
/*
Plugin Name: My Custom Python Plugin
Description: This plugin lets you run Python scripts in WordPress.
Version: 1.0
*/

function run_python_script($script_name) {
    $output = exec('python ' . plugin_dir_path(__FILE__) . 'python-scripts/' . $script_name);
    return $output;
}

$output = run_python_script('example.py');
echo $output;
?>

This, of course, is just an example that echoes the output to the top of the screen, which isn’t very usable for a real-world implementation.

To make it usable, you could refactor the plugin to allow you to run any Python script uploaded to the “python-scripts” folder with a shortcode (the shortcode will return the result of the Python script’s execution):

<?php
/*
Plugin Name: My Custom Python Plugin
Description: This plugin lets you run Python scripts in WordPress.
Version: 1.0
*/

function run_python_script($script_name) {
    $output = exec('python ' . plugin_dir_path(__FILE__) . 'python-scripts/' . $script_name);
    return $output;
}

add_shortcode( 'run_python', 'run_python_shortcode' );
function run_python_shortcode($attributes) {
    $script_name = $attributes['script'];
    $output = run_python_script($script_name);
    return $output;
}
?>

To run the script in a post, page, or any other content type, use the shortcode below:

[run_python script="example.py"]

And to run it inside your theme (or in another plugin), use WordPress’ do_shortcode() function:

<?php
echo do_shortcode('[run_python script="example.py"]');
?>

Use a Python Hosting Service

Another way to run Python in WordPress is to use a cloud hosting service for your Python script, like PythonAnywhere, which starts at $5/month, and integrate it with your website.

To integrate a Python script hosted on PythonAnywhere with a WordPress website, you’ll need to create a REST API endpoint that your WordPress site can call to retrieve the data generated by the Python script.

Here are the steps involved:

  1. Host your Python script on PythonAnywhere. First, you’ll need to upload your Python script to PythonAnywhere’s cloud hosting service. You will also need to configure the hosting service to run your script as a web app.
  2. Create a REST API endpoint. Next, you’ll need to create a REST API endpoint that returns the data generated by your Python script. This can typically be done using a web framework like Flask, which provides an easy way to create a REST API.
  3. Call the REST API from WordPress. Finally, you’ll need to call the REST API from WordPress to retrieve the data generated by your Python script. This can be done using the wp_remote_get() function, which allows you to make HTTP requests from WordPress.

Here’s an example for you would do that:

<?php
$response = wp_remote_get('https://your-rest-api-endpoint.com/');
$output = json_decode(wp_remote_retrieve_body($response), true);

// Do something with the output generated by the Python script
echo $output['example_key'];
?>

In this example, the wp_remote_get function is used to make an HTTP GET request to the REST API endpoint. The response is then decoded using the json_decode() function and stored in a variable.

Last but not least, the data is echoed to the screen.

By Dim Nikov

Editor of Maker's Aid. Part programmer, part marketer. Making things on the web and helping others do the same since the 2000s. Yes, I had Friendster and Myspace.

4 comments

    1. Hi, Tahir,

      If you mean the Python library, the answer would depend on your server. I recommend checking in with your hosting provider.

      Dim

Leave a comment

Your email address will not be published. Required fields are marked *