What is WordPress? A technical definition is “WordPress is an open source content management system (CRM) built in PHP”. Currently WordPress is the most popular CRM available for free! If you’ve ever used WordPress, you definitely know about themes & plugins in WordPress! In this article I will show you how to create a simple WordPress plugin in less than 15 minutes! But first, let’s talk more about what a plugin actually is.
WordPress plugin is a package of PHP script(s) that can alter your website. A plugin can alter your website in many ways i.e. From adding a simple message in header to creating a whole shop on your website eg. WooCommerce. Plugins can also modify/tweak existing features of your website like changing Login, sending email on specific event etc.
WordPress theme or Plugin?
If you have ever opened a WordPress theme, you’ll know that every theme contains a function.php file. This file contains all the theme related logic and any functionality provided by a particular theme. You can modify this file and add your own custom functionality too. So if we can directly add new feature in function.php, why do we need a plugin? Well the answer is simple, if you only want to add some text in the header or something small like that, you can surely use theme’s function.php. If you want something like a custom chat bot or anything like that, then a plugin would better suit your needs.
The main difference between a theme’s ‘function.php’ and separate plugin is that plugin’s functionality persists regardless of your website’s current theme but any changes from function.php will only be applicable when that particular theme is in use.
Creating our first WordPress Plugin
WordPress plugin’s default structure only requires one PHP file in a separate directory with a PHP block comment containing various data about that plugin. So, to create a plugin you need to navigate to your wp-content/plugins folder. Create a new folder name myFirstPlugin. Inside this new folder create a new file named myfirstplugin.php. Open the created file in any text editor of your choice and paste the following in it.
Plugin Name: My First Plugin
Plugin URI: http://mywebsite.com/
description: My first ever WordPress plugin
Author: Arman Khan
Author URI: https://instagram.com/codingwitharman
Here, only Plugin Name is required, but it’s a good practice to provide as much data as possible about our plugin.
That’s it!! You’ve created your first ever WordPress plugin. You can log in to your wp-admin and go to plugins and active this plugin. Of course, this plugin does not do anything, yet! But it is an active plugin.
How to create a great plugin?
There are some parameters that you should consider while creating your plugin. These parameters will help you create a successful well documented plugin.
How to structure your plugin? Well, this question is quite common among new WordPress developers.
If your plugin provides complex functionality, you should divide it in multiple files. For example, if your plugin has a main class, you should put that in your plugin’s main file and provide connections to other files through it. If your plugin includes UI related files, you should consider sorting them in separate folders like JS/CSS/Images and put all these folders in one assets folder. Well of course this is not a requirement from WordPress but a good plugin structure is always preferred compared to a bad one.
When creating functions in the plugin, you should be very careful naming them. You should never choose more general names for your functions as it might clash with other plugins that have similar functionalities.
The most common solution is to use unique prefix. For example, if your function name is commonFunction then you should replace it with something like ak_commonFunction.
This function runs when a WordPress administrator delete your plugin from backend. This is a great way to remove plugin specific data like tables in database or custom field created by your plugin.
Well, so now you know how to create a simple WordPress plugin. Now you can easily follow articles that require you to create WordPress plugin first. Of course this guide does not contain advance features that a plugin can provide, we’ll cover those topics in future articles for sure. Stay tuned!
Why should I learn PHP 7 in 2020?
PHP is compatible with all leading operating systems such as Windows, Linux, MAC OS and many others. Likewise, it is compatible with all popular web servers such as Apache, NGINXm IIS etc. PHP7 offers great compatibility and performance. Not only is it open source but also it contains many features and it is absolutely easy to install and set-up. We will cover installation part later in this article.
What are features of PHP?
PHP comes with many benefits that let you create wonderful web applications.
1. Open Source
PHP is an open source programming language which means it is free to use. There is a great community to help you if you get stuck anywhere and not able to figure out the problem.
PHP supports all major operating systems and servers.
3. Easy to use
PHP is simple and clean scripting language. Its syntax is convenient to use. Anyone can quickly learn PHP.
It is one of the fastest programming language. It takes so little time to establish a connection with database or fetch data from web services.
How to install PHP7?
You can follow this guide to install PHP7 in your preferred operating system.
Step 1 – Download PHP7
Go to the official PHP website and download the latest version. Save it on your windows PC.
Step 2 – Extract the zip
Step 3 – Setting up Environment variable
We need to configure the environment variable to access PHP from the command line. You can follow these steps to do so.
Right click -> My Computer (This PC) -> Properties -> Advanced system settings
This will open a the following window.
Now click on Environment variables, select path under the System variables section and edit. Add the path of installed PHP to system path.
Click on the new button and add the path to the PHP bin. Save all changes and close all the windows. Now to verify the settings, open console (or CMD) and execute following.
If you get the following output, it means PHP is successfully installed on Windows.
Here, we will see the steps to install PHP in Ubuntu Linux.
Step 1 – Update Ubuntu
Before installing anything new, you should always update your system. Open a terminal and run the following commands:
apt-get upgrade && apt-get update
Step 2 – Install PHP7
You can install PHP7 from Ubuntu’s official repository by following command:
apt-get install php
This command will install PHP7 and it’s required dependencies. To check whether PHP is installed or not execute following command:
It will return the PHP version related information.
PHP7 Syntax Overview
Like any other programming language PHP has a set of syntax rules which we need to follow while programming. PHP files commonly have a .php file extension as they’re always executed on server.
Tags in PHP or Escaping to PHP
The PHP parsing engine needs a way to differentiate PHP code from other elements. PHP tags are used for this.
We can ‘Escape to PHP’ by using the following 4 methods:
Canonical PHP tags
SGML (or) Short HTML Tags
HTML Script Tags
ASP Style Tags
From all of the above, Canonical PHP tags are most commonly used and are compatible with majority of servers.
# Here the echo command prints the message
echo "Hello world";
Comments in PHP
Comments are statements in a program by developers which describe the intent and purpose of the code. There are 2 styles for comments in PHP. Comments in PHP code are not read or executed by the PHP engine. Therefore, they do not affect the output.
Single Line Comment
// This is a single line comment
echo "Hello World";
# This is also a single line comment
Multi line Comment
Example for multiline comment
This is a multiline comment
echo "Hello world";
PHP Syntax is Case-Sensitive
In PHP besides variables, all other keywords are not case-sensitives. We have to be very careful while defining and using variables. Look at this example.
// Here all echo statements will execute
// in the same manner because keywords are case-insensitive
$variable = "Test";
// You will get an "Undefined Variable" error
// Because variable names are case-sensitives
If you want to build a fast and beautiful Frontend for you PHP-Backend Application, consider the popular Framework Vue.JS 2 Course.
In this tutorial we are going to have a Moodle Instance up and running on a Virtual Machine – inside Docker Containers.
First of all, install and get Docker up and running. Then, download Moodle or make sure you have a Git-Repository ready at hand. We are going to use docker-compose for this task. Now let’s try and Run Moodle with Docker.
Moodle with Docker
We need a container that will have all the needed technology to run and serve the Moodle source code (your repository). This configuration will come from an Image, that we will use and configure through a docker-compose.yml file. Create a file docker-compose.yml and insert the version of docker-compose syntax that we are going to use:
These Technologies are needed:
Operating System (Ubuntu)
Server (Apache2, Nginx)
PHP7.x (incl. php-curl, php-mysql, etc.)
Database Docker Image
We are going to use a simple and straightforward solution from bitnami. We simple create a service that will use this image, and set some parameters like username, db_name, etc.
We call our service ‘mariadb’, expose some ports (3306) and bind a volume to persist our data on our host machine. We also attach ‘mariadb’ to a network moodle-net. Also, make sure that docker can read/write the /home/maria_data folder on your host. We set rwx access for now (which is not recommended), please make sure to set the proper rights.
We specify the image that we tested previously. Specify the network to be same as the network of mariadb container. The Env Vars that we define, will be used by moodle in config.php. We open ports and specify the volumes. We make sure that moodle container only starts after mariadb container is up with the depends_on option. Make sure /home/moodle_data is writable.
sudo chmod a+rwx /home/moodle_data/
Before we continue, in these courses you can learn how to create backend and frontend application. Both of which you can easily deploy and serve with Docker. And now you know how to use docker-compose. Expand your skill-set with these courses.
At last, we need to define our network and volumes:
Now let’s boot up these containers with and visit localhost or your public ip.
docker-compose up -d
You should see the installation process. Enter your Database data (see ENV VARS in MariaDB Service. After that a config.php file will be created. If not, copy the code and create a config.php file in root folder.
After finishing up the setup process of moodle, you’ll finally see your ready working moodle instance. Now you run Moodle with Docker Containers.
After setting up moodle. Your host volumes/folders will be filled with data that comes from containers. E.g. moodle population a predefined folder structure in moodle_data.
cache filedir lang localcache lock moodle muc sessions temp trashdir
Same, and more importantly, applies to mariadb container. Our Database is now persisted on our host machine.
aria_log.00000001 aria_log_control bitnami_moodle ibdata1 ib_logfile0 ib_logfile1 multi-master.info mysql mysql_upgrade_info performance_schema tc.log test
German IT Academy Coach
Andrey Bulezyuk is the founder of German IT Academy and a course creator. He mainly covers topics from Web Development (NodeJS, VueJS, Django etc.) to Data Science (Pandas, Numpy) and Machine Learning (Tensorflow, Keras, etc.).
He published a German book “Algorithmic Trading“, giving his readers the opportunity to learn how to code automatic trading systems for the stock market or the Forex.
He has experience as a Backend and Frontend Developer. In his early years he worked on Web Projects as a Freelancer. Switched later to Financial Market System Developing. Currently employed as Machine Learning Engineer.
He takes part in different projects and acts as a Co-Founder in different small start-ups. Andrey Bulezyuk was an employed Full-Stack Developer in Munich. Currently his duties do cover areas of Machine Learning and ETL.