How to Run Python Scripts on WordPress ?

WordPress, as we all know, is one of the most popular free and open source content management system based on PHP and MySQL. At the time of writing this article WordPress, 4.9 has been downloaded 153,263,827 times and it’s still growing.

On the other hand, Python is an interpreted high-level programming language for general-purpose programming. Created by Guido van Rossum and first released in 1991, Python has a design philosophy that emphasizes code readability, notably using significant whitespace. Due to its elegant syntax, it’s really easier to read, which make it one of the most popular programming language.

Now, what if you want to use Python scripts in WordPress.. is it really possible ? ….

Well, the answer is yess… in this article .. we gonna show it .. how can you use .. python scripts in a WordPress blog.

So let’s create a python file  (.py) and store it aside for now. Now let’s create a WordPress plugin  and register a shortcode:

<?php # -*- coding: utf-8 -*-
/* Plugin Name: Python embedded */

add_shortcode( 'python', 'embed_python' );

function embed_python( $attributes )
    $data = shortcode_atts(
            'file' => ''

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

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

    pclose( $handle );

    return $read;

Now you can use that shortcode in the post editor with [python] or [python file=""].

Put the Python scripts you want to use into the same directory as the plugin file. You can also put them into a directory and adjust the path in the shortcode handler.


How to Run Python Scripts on WordPress ?
You may Also Like
Scroll to top