Create a Simple WordPress Plugin

The WordPress CMS has changed the face of our Internet and allowed a surge of new ideas to prosper and its open source movement holds a strong presence rooted in software and web development. WordPress is a blogging platform which has the ability to launch into many other scripts such as web forums, job boards, and even a classic webpage Content Management System.

i) Basic Structure

A WordPress plugin can consist of as little as a single PHP file however for consistency I always create folder and at least the single PHP file within it. The folder and the file should have the same name with the exception of the file extension. We’re going to call our plugin “My Cust Function” so the directory and file would be named accordingly.

A) Folder: ikodes-plugin
B) File: ikodes-plugin.php

When we are done we are going to upload this newly created folder with the file inside it to our wp-content/plugins directory.

ii) Basics of a WordPress Plugin file

There are no doubt endless conversations that could be had about all the things that can go into your plugin file but I’m going to cover the absolute necessities to get you started quickly.

iii) Plugin Header

<?php
/*
Plugin Name: Example Plugin
*/

At this point you have a plugin that you can activate in your WordPress plugins area. Of course our plugin doesn’t actually do anything yet but the point is you have laid the foundation of your very own plugin and it was super easy
There are other elements that you can include in this header. Things like a description, version, author, etc.

iv) The rest of ikodes plugin

As I said earlier there is really no end to what you can place inside your plugin but at this point, at a very basic level, you can think of it like your themes functions.php file. By that I mean that if you did nothing else you could place all those little code snippets that you love so much into this file instead of in your functions.php file and they would work exactly the same way.

function ikodes_redirect () {
global $post;
if ( is_page() || is_object( $post ) ) {
if ( $redirect = get_post_meta($post->ID, ‘redirect’, true ) ) {
wp_redirect( $redirect );
exit;
}
}
}
add_action( ‘get_header’, ‘ikodes_redirect’ );

After adding above code your file code look like below …

<?php
/*
Plugin Name: Example Plugin
*/

function ikodes_redirect () {
global $post;
if ( is_page() || is_object( $post ) ) {
if ( $redirect = get_post_meta($post-&gt;ID, ‘redirect’, true ) ) {
wp_redirect( $redirect );
exit;
}
}
}
add_action( ‘get_header’, ‘ikodes_redirect’ );

As you can see this plugin is not complicated at all and the best part is if something seems to be going fishy with my redirects I don’t have to dig through a huge file of miscellaneous functions to find it. In fact I could have named this plugin My Redirect and then if anything happened I could just deactivate this one plugin without having an adverse affect on my entire site.

 

Related posts