Run Python Script on WordPress Website

This issue of permission denied is resolved.. Permission was already given but I have added ‘env python’ in the popen function.. Now the code is running as expected..

add_shortcode( 'run_test_py', 'test_py' );

function test_py( $attributes )
{
    $data = shortcode_atts(
        [
            'file' => 'test.py'
        ],
        $attributes
    );

    $handle =  popen('env python '. __DIR__ . "https://wordpress.stackexchange.com/" . $data['file'] . ' 2>&1', 'r' );
    $read = '';

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

    pclose( $handle );

    return $read;
}

Leave a Comment