Connect with us

How to create a plugin for WordPress?

coding

How to create a plugin for WordPress?

UPDATE: For a complete guide on how to create a WordPress plugin (that sells), please check out our newly released tutorial on Teachable: https://coderevolution.teachable.com/p/wordpress-plugin-development-tutorial.

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 define the plugin file header, in the file that will be considered the plugin’s ‘main’ file. It will give details about the plugin to the WordPress on which the plugin resides, like plugin name, URI, description, version, author info, text domain and also 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
/*  
Plugin Name: PLUGIN_NAME
Plugin URI: PLUGIN_URL
Description: PLUGIN_SHORT_DESCRIPTION
Version: PLUGIN_VERSION
Author: PLUGIN_AUTHOR_NAME 
Author URI: PLUGIN_AUTHOR_URL
Text Domain: PLUGIN_TEXT_DOMAIN
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("CodeRevolutionDemoPlugin") )
{
class CodeRevolutionDemoPlugin
{
}
}

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("CodeRevolutionDemoPlugin") )
{
class CodeRevolutionDemoPlugin
{
public function __construct()
{
register_activation_hook( __FILE__, array( &$this, 'install' ) );
register_deactivation_hook( __FILE__, array( &$this, 'remove' ) );
register_uninstall_hook( __FILE__, array( &$this, 'uninstall' ) );
}
public function install()
{
// install/set stuff
}
public function remove()
{
      // remove stuff
}
public function uninstall() 
{   
// 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("CodeRevolutionDemoPlugin") )
{
class CodeRevolutionDemoPlugin
{
public function __construct()
{
register_activation_hook( __FILE__, array( &$this, 'install' ) );
register_deactivation_hook( __FILE__, array( &$this, 'remove' ) );
register_uninstall_hook( __FILE__, array( &$this, 'uninstall' ) );
add_filter( 'the_content', array( &$this, 'createLike' ) );
          }
          public function install()
          {
                 // install/set stuff
          }
          public function remove()
          {
                 // remove stuff
          }
public function uninstall() 
{   
// 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.

Continue Reading
You may also like...
Click to comment

Leave a Reply

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

More in coding

To Top