0

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?

2 Answers 2

0

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
/var/www/html/wordpress/wp-content/plugins/run-python
# cat hello.py 
#!/usr/bin/env python3
print("Hello")
# chmod +x hello.py
# ./hello.py 
Hello

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 
<?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
"
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.

<?php
/**
 * 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(
        array(
            'file' => 'hello.py',
        ),
        $attributes
    );

    $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.

Note

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.

2
  • 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.
    – nickh
    Commented Jun 29, 2020 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
    Commented Jun 29, 2020 at 11:04
0

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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