How to Create a Simple WordPress Plugin

[Fuente: http://wpninjas.com/how-to-create-a-simple-wordpress-plugin/]

When giving support or offering various snippets we frequently advise people to create a WordPress plugin to handle all of their modifications. Here is how you create a very simple plugin to house all your little snippets and modifications.

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 Custom Functions” so the directory and file would be named accordingly.

  • Folder: my-custom-functions
  • File: my-custom-functions.php

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

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.

WordPress Plugin Header

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

1
2
3
4
<?php
/*
Plugin Name: My Custom Functions
*/

See how easy that is? 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.

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 your 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.

1
2
3
4
5
6
7
8
9
10
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 altogether now.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
/*
Plugin Name: My Custom Functions
*/
function my_custom_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’, ‘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 affect on my entire site.

 In Summary

You can no longer say that it’s to difficult to create a plugin. It’s just as easy as adding code to your functions.php, which you should almost never do, and so much cooler too.

Bonus Round

If you are interested in digging into creating WordPress plugins more complicated than what I describe above consider checking out the following resources.