Create a WordPress plugin

Wordpress - create a plugin
Difficulty

It is not uncommon to look for the perfect plugin/functionality for your blog/site in WordPress but not find it because you have too precise an idea of what you would like. It is in these cases that the possibility of infinitely extending WordPress comes to our rescue. We can add features that we deem necessary for us and for others.

The first ingredient when you want to create a plugin is clearly to have a personal test installation of WordPress. We will insert our plugin inside it to make tests before publishing it.

Once you have that, you can create a new folder inside it for your plugin, with the name you want to give it.

/wp-content/plugins/my-new-plugin


Within that, we must create a PHP file. It will be our main file, which will contain the basic information of our plugin. We have to add a comment at the top of the file, as below.

<?php
/**
 * Plugin Name: The name of your new plugin.
 * Plugin URI: The url of your new plugin.
 * Description: The description of what this plugin can offer to the world.
 * Version: 1.0.0
 * Author: Your name or nickname
 * Author URI: The url of your personal website or portfolio.
 * License: GPLv2
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 **/


Together with the description of our plugin we insert some definitions of security and service.

defined( 'ABSPATH' ) or die( 'Unauthorized Access!' );

if ( !defined('MYPLUGINNAME_PLUGIN_DIR') ) {
  //DEFINE SOME USEFUL CONSTANTS
  define('MYPLUGINNAME_PLUGIN_VER', '1.0.0');
  define('MYPLUGINNAME_PLUGIN_DIR', plugin_dir_path(__FILE__));
  define('MYPLUGINNAME_PLUGINS_URL', plugins_url('', __FILE__));
  define('MYPLUGINNAME_PLUGINS_BASENAME', plugin_basename(__FILE__));
  define('MYPLUGINNAME_PLUGIN_FILE', __FILE__);
  define('MYPLUGINNAME_PLUGIN_PACKAGE', 'Free');
  
  // Your plugin code...
}


Inside it we can insert the basic functions of our plugin, calling up any other php files if necessary to better order our code, via require_once.

This can be done by adding functionality via WordPress hooks.

As an example we could insert additional text into the content via the hook filter.

  if (!function_exists('mypluginname_add_to_single_content')) {
    function myplugin_add_to_single_content($the_content)
    {
      return $the_content . 'Hello world!';
    }
  }
  add_filter('the_content', 'myplugin_add_to_single_content', 20);


For publication, 2 more files will have to be added inside the plugin folder:

  • license.txt
  • readme.txt

The license.txt file should contain the following text regarding the license you want to associate with our plugin.

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


The readme.txt file must contain useful information for publication.

=== My Plugin Name ===

Contributors: Your name
Version: 1.0.0
Tags: tags, to, find, your, plugin
Requires at least: 5.0
Requires PHP: 7.0
Tested up to: X.X
Stable tag: main
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html

A short description.


== Description ==
A long description.


== Installation ==
* First installation point.
* Second installation point.


== Frequently Asked Questions ==
* Questions for your plugin.

== Translations ==
* Language.


== Changelog ==

Releases.

Now you can publish your new plugin and let the world see and use it.
To do this you can go to the site:

https://wordpress.org/plugins/developers/add/

That’s all.
See you next time.

0
Be the first one to like this.
Please wait...

Leave a Reply

Thanks for choosing to leave a comment.
Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published.
Please do NOT use keywords in the name field. Let's have a personal and meaningful conversation.