Creating A Simple WordPress Plugin

create-a-simple-wordpress-p

Having problems modifying your WordPress templates? Having a hard time applying the changes that you want? Well, you have two options. One is to create a child theme where you can put all your modifications and the other one is to create a WordPress plugin where you can put all your modifications as well.

In this tutorial, we will create a simple WordPress plugin to provide you with an alternative when it comes to housing your snippets and modifications. Let’s begin:

Basic Structure of a WordPress Plugin

A WordPress plugin can consist of as little as a single PHP file but for consistency, let’s create a 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 Custom Functions” so the directory and file would be named accordingly.

Folder: example-plugin
File: example-plugin.php

When we’re done we’re going to upload this newly created folder with the file inside it to your wp-content/plugins directory on our server.

WordPress Plugin Header

Here is all that needs to be in your plugin file for WordPress to recognize it once uploaded.

——————————————
/*
Plugin Name: Example Plugin
*/
——————————————

Easy right? At this point, you have a plugin that you can activate in your WordPress plugins area. But of course the 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.

Now there are other elements that you can include in this header. Things like a description, version, author, etc. You can read more about those here: http://codex.wordpress.org /Writing_a_Plugin #File_Headers

The Rest Of The WordPress 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.

As an example consider this little snippet that I sometimes use when I want to very quickly be able to redirect a page completely to a different page whether on the same site or another site entirely.

——————————————
function my_custom_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’, ‘my_custom_redirect’ );
——————————————

This snippet is very glamorous but what it essentially does is allow me to add a piece of custom post meta to any page called “redirect” with a URL. When the page is called and has this custom post meta then it’s automatically redirected to the new URL. Let take a look at it all together now.

——————————————
/*
Plugin Name: Example Plugin
*/

function my_custom_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’, ‘my_custom_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 Custom Redirect and then if anything happened I could just deactivate this one plugin without having an adverse effect on my entire site.

There are a lot of things you can do with it. Just look online for some functions that you wanted to add and put it here. Goodluck.

Vhic Hufana II

<p>Vhic Hufana is ReBuildSEO’s owner and founder, an SEO consulting firm. He is an SEO, web designer and coder.</p>

Leave a Reply

Your email address will not be published. Required fields are marked *