Introduction to writing a WordPress plugin

If you step into the web development world using HTTP (Hypertext Transfer Protocol) language, almost every file you created in “those days” must be in .html or .htm format. the Internet WWW (World Wide Web) infrastructure has changed at a rapid rate since the introduction of standardized HTTP language.

Skip Tech Jargon

Without a header, it is just a code file.

This is because the limitations of HTTP language can hinder the performance of large scale websites. What really pushed the market towards server side programing languages such as PHP(Personal Home Page/Hypertext Preprocessor) is the drop in equipment costs and rapid growth in “personal” online space. The greatest outcome of this growth in the large scale web industry is the CMS (Content Management System) platforms. (Note, we still use HTML tags in server side programming except with added complex codes.)

Why do we use CMS?

Almost all CMS programs are written in server side languages such as PHP, .Net, etc. It reduces the load on the client computer and avoid(if not reduce) the client side dependencies. For example, if you want to create a website in Arial font with text size of exactly 15 px, you can dictate that in a CSS (Cascading Style Sheets) script and load it into a PHP code. This is just an example of many more advantages of server side programming.

The key major advantage of using a CMS is the flexibility it can bring to a website. CMS programs such as Drupal and Wordperss is written in PHP and styling codes are often loaded with CSS scripts. In fact, PHP and CSS works together like a hand to a glove. The WordPress(WP) CMS is written in PHP and supports PHP/CSS scripts. WordPress is an open source project with very flexible back-end, including but not limited to themes and plugins.

Let’s get started

Now we have the background, let’s get started on writing a WP plugin. Plugins are intended to modify the per-configured WordPress structures and/or to introduce new functions to the existing structure. The following basic plug in code is written in PHP.

<?php
/*
Plugin Name: Sanuja Custom Tags 
Plugin URI: https://sanuja.com/blog/custom-wp-tags-control
Version: 0.9
Author: Sanuja Senanayake
Author URI: http://www.sanuja.com
Description: Changes the font sizes used by the tag cloud widget.
*/

?>

The file is saved as a FileName.php in an appropriate folder within the WordPress directories. You can either create a .zip folder with all the required files and folders and load it to the CMS using Plugins uploads OR you can just save the file in the right folder through FTP or other means of back end access (e.g. cPanel). We will talk about these in detail later sometimes.

Oh well, rules are rules

On the first two lines of the code, you will notice that we open with the PHP open tag followed by a comment type information. This is known as header information. This header will make it a plugin. Without this information, the file will just contain bunch of CSS, PHP, JavaScript, so on codes. This header is very specific in it’s order and how it is written. The specifications can be summarized as followed;

  • Plugin Name: title of the program usually in English but can include ANY UTF-8 characters
  • Plugin URI: web address pointing to the plugin specific page, but can be any URL
  • Version: version update information
  • Author: programmer’s name usually in English but can include ANY UTF-8 characters
  • Author URI: web address to the author’s website, but can be any URL
  • Description: information about the functions of the plugin

Once you fill typed in the header, you can go ahead and save the file as a .php file. There are few rules you must follow in order to ensure the plugin file meets the requirement for WordPress. These include, but may not be limited to;

  • File name must not contain any type of white space(tab, space, blank spaces, etc)
  • Folder name you will be given to the plugin must be unique to itself
  • While you may have two or more plugins with the same .php name and/or title, it is highly unprofessional to do so
  • Should not contain any codes that could interfere with WordPress functions.php or other core php files.
  • Must add value to the CMS without causing major errors
  • A good plugin is always plug and play(end user should not have to modify the code itself in the editor)

I will build up on this code on my next lesson on WP plugin next time. Until then have a codegasm 🙂

One thought on “Introduction to writing a WordPress plugin”

Comments are closed.