Create a Custom Plugin for WordPress

Create a Custom Plugin for WordPress
Create WordPress Custom Plugin Mobirise

If you use WordPress, you’re familiar with plugins: those third-party programs that extend or modify WordPress in both big and little ways. Public plugins are available through the plugins directory or by download directly from the developer, and are designed for general use.

But plugins don’t have to be public. If you’re a WordPress developer, creating a plugin file can be useful for day-to-day customization.

Why create a private plugin?

There are at least three instances where you would want to create a private plugin:

  1. There are some action hooks that are only available to plugins;
  2. It’s the only way to add javascript to administration pages;
  3. It lets you put your custom functions in a file that won’t be deleted if a theme is trashed or changed.

What makes up a plugin?

A plugin is just a collection of files placed in a special folder in the WordPress directory, with metadata that tells WordPress it’s a plugin. They can be as simple or as involved as you want.

Getting started

  1. Create a folder for your plugin in wordpress/wp-content/plugins/
  2. Give it a unique name — something like “Clientname_custom_functions”
  3. Inside that folder, put a PHP file with the same name: “Clientname_custom_functions.php”

If the plugin will be referencing other files (images, javascript, CSS, etc.), they’ll end up in this folder, too.

The metadata

At the top of the PHP file, put the following:

<?php
/*
Plugin Name: My custom functions
Plugin URI: http://businessfinder.ae
Description: Custom functions for UAE Business Directory.
Author: Your Name
Version: 1.0
Author URI: http://businessfinder.ae
*/
?>

Customize the above entries with your own data. The URIs are optional.

The code

Put your custom code below the metadata. You’ll need to attach it to a WordPress hook (see below) so WordPress knows when to run it.

That’s it! You’ve just created a plugin.

Activating the plugin

  1. Upload your plugin folder to the website
  2. Go to the Plugins page in the dashboard
  3. Find your plugin in the list and click “Activate”

WordPress hooks: Actions and filters

WordPress provides lots of “hooks” into its core programming. By attaching your code to a hook, you control when in the page-build process the code runs.

There are two kinds of hooks: actions and filters.

Actions are for doing something. You call them using the function add_action(). A list of action hooks is here:

https://codex.wordpress.org/Plugin_API/Action_Reference

Filters are for filtering data. Filter hooks are connected to places in the process where data has just been accessed. You call them using the function add_filter(). A list of filter hooks is here:

https://codex.wordpress.org/Plugin_API/Filter_Reference

To use a hook, you first write your function. Then you use add_action() or add_filter() to tell WordPress when the function should run. For example:

<?php
function do_something {
    echo "do something!";
}
add_action('wp_head','do_something');
?>

The add_action() line tells WordPress to run the function do_something at the wp_head action hook, which fires when a page’s header file is loaded.

Playing nice with others

Because all plugins play in the same sandbox, it’s possible to run into problems if you’re not careful. For example, if two plugins try to call a function with the same name, you’ll get ugly behavior. There are three main things to watch for:

Plugin name

Give your plugin and its plugin folder a unique name. Preface it with your initials, for instance.

Check before overriding WordPress functions

You can override certain native Wordpres functions by writing a function with the same name. But check to make sure the function hasn’t already been overridden; otherwise you’ll get a fatal error.

To perform this check, wrap your function in some conditional code:

if ( !function_exists('wp_new_user_notification') ) {
wp_new_user_notification() {my override version here}
}

 

This ensures that your version of the function is used only if no other custom version is already being used.

If a user loads two plugins that override the same WordPress function, only the first plugin to load will succeed.

Give your functions unique names

Just like your plugin needs a name that isn’t likely to be duplicated, so do your functions. If you’re building a custom private plugin, for instance, you might include the client’s name in the function name. Or a random 10-digit number.

Credit: Steven Ray