Multiple versions of Python

Do you have multiple versions of Python to be installed? Do you have additional python packages that must be used in separate versions of Python installed on the system?

Use Virtual Environments!

Today we will see how to setup 2 different versions of Python environment.

  • Install python

    sudo apt install python

  • Install pip (python’s package management system):

    sudo apt install python-pip

  • Upgrade pip:

    pip install –upgrade pip

  • Install virtualenvwrapper by running this command:

    pip install virtualenvwrapper

  • Before we can create a virtualenv, we need to run the virtualenvwrapper script. We can find the location of virtualenvwrapper script by running the following command:

    which virtualenvwrapper.sh

  • Execute the script as:

    source ~/.local/bin/virtualenvwrapper.sh

  • Make a virtual environment with following command

    mkvirtualenv -p <location of python3.5> <name_of_env>

    For example for creating virtual env for python 3.5 named ‘venv35’, run:

    mkvirtualenv -p python3.5 venv35

    It will take activate the virtualenv for us directly. To deactivate it, enter:

    deactivate

  • Similarly, we can create another environment for python 2.7 named ‘venv27’, run:

    mkvirtualenv -p python2.7 venv27

  • To run, any environment, run the command:

    workon <env name>

    For exaemple, to run venv27, use:

    workon venv27

Enjoy!

Creating Our First WordPress Plugin

So you finally decided to create your own plugin. But don’t know where to start?
Well in that case I can only say-

“Welcome Aboard’ 🙂

If you have searched on the Internet for plugin development, like I did, you would certainly be over-whelmed with so many tutorials and guides. And you can’t decide which one is better. Let me tell you, all of them are good and useful but certainly not the best. What?! Yes, you read that right. Nothing’s ever perfect, there’s always a need for improvement. Its the law of development.

Before you say anything, this tutorial falls in that same category. What I’ll teach you today is just the basic stuff to get you started in this field of WordPress plugin development.
Anyway, let’s begin with our tutorial. But first, let’s get the definition out of the way.

Plugin: In computing, a plug-in (or plugin) is a software component that adds a specific feature to an existing software application. When an application supports plug-ins, it enables customization.
(Source- Wikipedia)

To write a plugin for WordPress we must follow some guidelines that WordPress has provided. You can read about them here.
Ok! Here’s what we are going to do, we’ll create a plugin named ‘demo’ in this tutorial that will display the content ‘Hello! This is my first Plugin’ in any page or post that we want.

First let’s create a folder named ‘demo’ in our WordPress plugin directory. And then we create a file named ‘demo.php’ in it.
After creating it, we open that file in any text-editor and write the following code.

<?php
/* Plugin Name: demo
Plugin URI: http://demo.wordpress.com
Description: A demo plugin.
Version: 1.0
Author: Gurjot Singh
Author URI: https://bhattigurjot.wordpress.com
License: GPL2
*/
?>

We’ll save the file. You can write anything you want. But this information is very essential if we want our plugin to display in the Plugins section in our WordPress.

Now we can see our plugin listed among all other plugins. Yey! 🙂

Okay, moving on to the next part, we’ll create a simple HTML text in another PHP file.

We’ll now open a new document and write the following code:

<?php
function myfn() {
$html=' Hello! This is my first Plugin ';
}
?>

We’ll save this file as ‘function.php’ in our same demo directory.
We now have this function which contains html text but how do we display this text in our page?
To do this we must include this function.php file in our main ‘demo.php’ plugin file by adding the following code:

include ("function.php");

Even if we’ve included our ‘myfn()’ function in our demo.php we can’t display it as such. And we don’t even know where to display the text.

In order to do that we create a shortcode that we insert in any page or post to display our plugin’s content. To create our own shortcode we write the following in our demo.php file:

if ( !is_admin() )
add_shortcode('mydemo', 'myfn');

This creates a shortcode [mydemo] for our function myfn() that displays the text.
Now the complete demo.php file looks as follows:

<?php
/* Plugin Name: demo
Plugin URI: http://demo.wordpress.com
Description: A demo plugin.
Version: 1.0
Author: Gurjot Singh
Author URI: https://bhattigurjot.wordpress.com
License: GPL2
*/
include ("function.php");
if ( !is_admin() )
add_shortcode('mydemo', 'myfn');

?>

Ok so its time to activate our plugin.
After activating it we can insert our shortcode ‘[mydemo]’ in any page or post we want.

And now we’ll see our ‘Hello! This is my first Plugin’ text in that page or post.

That’s it Folks! We have successfully created our very own first plugin. CHEERS! 😀
If you are having some trouble then don’t hesitate to ask.
And do read WordPress codex for plugin development.
Writing a Plugin
Plugin API
Plugin Resources
These are very important.
Post your comments below and I’ll see you (text-ly) next time. 😛 Till then Keep Developing!

WordPress Errors and their solutions

Error:
$ sudo gzip -d /usr/share/doc/wordpress/examples/setup-mysql.gz
gzip: /usr/share/doc/wordpress/examples/setup-mysql.gz: No such file or directory

Sol:
The problem is the setup-mysql file has to be unzipped.
Code:
$ sudo gunzip /usr/share/doc/wordpress/examples/setup-mysql.gz
Then retry
$ sudo gzip -d /usr/share/doc/wordpress/examples/setup-mysql.gz
Problem will be solved!

Error:
Neither /etc/wordpress/config-localhost.php nor /etc/wordpress/config-localhost.php could be found.
Ensure one of them exists, is readable by the webserver and contains the right password/username.

Sol:
Create the file manually if it doesn’t exist.
But if it is there, then run
$ sudo ln -s /etc/wordpress/config-localhost.php /etc/wordpress/config-default.php

Error:
Error connecting to the Database

Sol:
Make a backup copy of wp-config.php file.
then run
$ sudo cp /usr/share/wordpress/wp-config-sample.php /usr/share/wordpress/wp-config.php
$ sudo gedit /usr/share/wordpress/wp-config.php
then enter your
database name
databsae user
database password
localhost name
save that file.

Error:
You can’t manually install themes and plugins and also automatic updates doesn’t work.

Sol:
For automatic updates to occur, the folder and all its files and subfolders must be owned by www-data:
$ sudo chown -R www-data /usr/share/wordpress
This command will automatically solve your theme and plugin installation problem.

Error:
Blank screen when you go to http://localhost/wordpress/

Sol:
This error usually occurs due to some faulty syntax or some irregularities with your file.
In the wp-config.php file, find the code
define(‘WP_DEBUG’, false);
set it to
define(‘WP_DEBUG’, true);
Now your browser will show the errors that could result in Blank Page Error.

Happy Pressing! 😀