I've read this question (and many more web pages) in order to figure out how to run a simple python script via a Wordpress plugin.

However, I couldn't do it: I always get a "Blank" output.

No errors were displayed.

How can I execute a python function via Wordpress plugin?


I tried it step by step and finally succeeded.

Step 1 - hello.py

I tried to create the hello.py file with Atom on Windows 10 and uploaded it to a Linux server for many times. It could run with the "python3 hello.py" command, but always failed to run when using the "./hello.py" command.

I had to create it on the Linux server and the "./hello.py" command worked.

# pwd
# cat hello.py 
#!/usr/bin/env python3
# chmod +x hello.py
# ./hello.py 

Step 2 - t-popen.py

I created the t-popen.py file with Atom on Windows 10 and uploaded it to the Linux server. This PHP file was used to make sure the popen() function worked.

# cat t-popen.php 

/* Add redirection so we can get stderr. */
$handle = popen( __DIR__ . '/hello.py', 'r' );
$read   = '';

while ( ! feof( $handle ) ) {
    $read .= fread( $handle, 2096 );

pclose( $handle );

var_dump( $read );
echo $read;
# php t-popen.php 
string(6) "Hello

Step 3 - The WP plugin

I created the run-python.php file with Atom on Windows 10 and uploaded it to the Linux server.

 * Try - Run Python
 * @package     Try\Run Python
 * Plugin Name: Try - Run Python
 * Plugin URI:
 * Description: Try to run a Python script in the WordPress plugin.
 * Version:     1.0
 * Author:      Box
 * Author URI:

add_shortcode( 'python', 'embed_python' );

function embed_python( $attributes ) {
    $data = shortcode_atts(
            'file' => 'hello.py',

    $handle = popen( __DIR__ . '/' . $data['file'], 'r' );
    $read   = '';

    while ( ! feof( $handle ) ) {
        $read .= fread( $handle, 2096 );

    pclose( $handle );

    return $read;

Step 4

I activated the plugin and tried to add a shortcode as [python] to a post.

The "hello" string showed in the content when I viewed the post.


The 3 files mentioned above are all located in the same directory which is "/var/www/html/wordpress/wp-content/plugins/run-python" in this case.

| improve this answer | |
  • 1
    Excellent answer! Thank you for contributing to this thread. 2 years forward and after many issues, I gave up on Wordpress + Python for the project I was working on and did it with VueJS + NodeJS. – N. H. Deutschland Jun 29 at 10:29
  • No problem. I have to make WordPress and Python work together in this week. PHP isn't proper for something that Python is very good at. ;-) Vue.js + Node.js is a good choice for Web sites/apps. :thumbsup: – Box Jun 29 at 11:04

You should not be able to run any script on your server from your PHP. This is a security nightmare you should avoid. If you just have to be able to communicate with some random script on your server you should restrict the ability to only trusted computers (best thing use a different site which uses a different php.ini for that) or find a roundabout way to do that like using write to a file monitored by the python script to pass parameters. (open socket in your python can also be an option).

If as you say in the comments you need to process the output on your local machine, just use a wget or whatever is its python equivalent to get the output from the site and then process it.

| improve this answer | |
  • Actually, I really need python to process the PHP output. Since I'm working at my Localhost, security shouldn't be a problem here. – N. H. Deutschland Aug 15 '18 at 5:25
  • edited to take this into account – Mark Kaplun Aug 15 '18 at 5:37
  • Thanks for your suggestion! Back to the answer, are there any online references for me to apply that "roundabout way"? – N. H. Deutschland Aug 15 '18 at 6:05
  • far from being a python expert but this seems to be the way to go stackoverflow.com/questions/24346872/… – Mark Kaplun Aug 15 '18 at 6:13
  • Hmm, I'll just go with open socket then. Cheers! – N. H. Deutschland Aug 15 '18 at 6:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.