How to Create a Plugin for WordPress

There are two ways of creating a wordpress plugin, the correct way and the common way. The common way is to just throw functions in the main PHP file that goes in the plugin folder and hope that everything works just fine in the millions of different types of wordpress installations. The correct way, is to create it as a class, which works just like the non-class version but in a more semantic, protected and compatible way.

There are 3 steps that must be taken in order to make start making your plugin. First you need to informe the license type of the plugin, commonly all wordpress plugins are GPL2 licenses, so if you want to keep with the default just use the text bellow

<?php
/*  Copyright YEAR  PLUGIN_AUTHOR_NAME  (email : PLUGIN AUTHOR EMAIL)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License, version 2, as
    published by the Free Software Foundation.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

Everything great so far, now lets start creating our class, but first make sure there is no other class with the same name.

// Make sure there is no bizarre coincidence of someone creating a class with the same name of this plugin
if ( !class_exists("PLulzPlugin") )
{
    class PlulzPlugin
    {
    }
}

The basics for every plugin is that it should have a install, deactivate and remove methods, in those places you can easily append some pré-configuration code or remove it after. So the code above would become

// Make sure there is no bizarre coincidence of someone creating a class with the same name of this plugin
if ( !class_exists("PLulzPlugin") )
{
    class PlulzPlugin
    {
        public function __construct()
        {
            register_activation_hook( __FILE__, array( &$this, 'install' ) );
            register_deactivation_hook( __FILE__, array( &$this, 'remove' ) );
            register_uninstall_hook( __FILE__, array( &$this, 'remove' ) );
        }
        public function install()
        {
            // install/set stuff
        }

        public function remove()
        {
           // remove stuff
        }
    }
}

That’s pretty much it, all you have to do is hook the methods of this new plugin class with what you want your plugin to perform on the wordpress system. Just use the construct method to associate actions/filters with plugin methods. Let’s say that we want, for example, to create a plugin that changes how the content of a post is presented to user and want to embed a Like Button before the content is displayed. The above class would then, become:

if ( !class_exists("PLulzPlugin") )
{
    class PlulzPlugin
    {
        public function __construct()
        {
            register_activation_hook( __FILE__, array( &$this, 'install' ) );
            register_deactivation_hook( __FILE__, array( &$this, 'remove' ) );
            register_uninstall_hook( __FILE__, array( &$this, 'remove' ) );
            add_filter( 'the_content', array( &$this, 'createLike' ) );
          }
          public function install()
          {
                 // install/set stuff
          }
          public function remove()
          {
                 // remove stuff
          }
          public function outputJS()
          {
                <div id='fb-root'></div>
                <script>
                      window.fbAsyncInit = function() {
                      FB.init({
                           appId : '{$APPID}', // App ID
                           status : true, // check login status
                           cookie : true, // enable cookies to allow the server to access the session
                           oauth : true, // enable OAuth 2.0
                           xfbml : true // parse XFBML
                       });
                   };
                   (function(d){
                       var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
                       js = d.createElement('script'); js.id = id; js.async = true;
                       js.src = '//connect.facebook.net/en_US/all.js';
                       d.getElementsByTagName('head')[0].appendChild(js);
                       }(document));
                </script>
          }
          public function createLike( $content )
          {
               // Make sure this is not a feed request
               if ( !is_feed() )
               {
                 $this->outputJS();
                 $newContent = "<fb:like send="false" layout="standard" width="450" show_faces="false" action="like" colorscheme="light"></fb:like>"
                 $newContent .= $content;
                       return $newContent;
                }
                return $content;
          }
     }
}

The code make a check to see if the current request is not a feed request, if not it will insert a Like button in all the posts, before the content of the post.

The outputJS method will output the code everytime you want to include the Like button.

For any question, trouble or help feel free to post bellow.

Compartilhar:

7 Comentários. Comentar

بوابة حلفا
31 de janeiro de 2012 22:02

plugins/seo-facebook-commentsu/seofacebook.php on line 32

Responder
seleb
29 de fevereiro de 2012 9:44

Thank You! I think this is for advanced user.

Responder
Iva T. Louise
12 de março de 2012 16:40

I found most informative and takes time for me to fully understand and apply yet, I intend to do.

Responder
andy
13 de maio de 2012 19:09

As a note though only static methods are allowed in the register_uninstall_hook()

Responder
Dave Ricotta
18 de maio de 2012 0:05

I notice the LIKE button is on the page – and the peoples names who hit the LIKE button pop up – and different names pop up. So where are they stored so I can see who hit the LIKE button?

Responder
Mohammed Omar Faruque
16 de novembro de 2012 20:12

Nice & Thank's.

Responder
Daniel Ndubuisi Timothy
25 de novembro de 2012 9:09

nyce job…wel done.

Responder
Comente