2024: Domino Containers - The Next Step. News from the Domino Container commu...
How to Create a Custom WordPress Plugin
1. From the Resource Library of Andolasoft.Inc | Web and Mobile App Development
Company
1
2. From the Resource Library of Andolasoft.Inc | Web and Mobile App Development
Company
2
Custom WordPress Plugin act as add-ons with additional functionalities or extending any
existing functionality to a website without modifying the core files. It helps the installation
of future updates without losing any core functionalities or customizations.
Why would you want to create a plugin?
All WordPress themes contain a functions.php file, which includes code that adds all the
functionalities to your site. It operates very similarly to the way a plugin works. you can add
the same code to either a plugin or functions.php file, and both will work for you.
Consider this scenario.
You have decided to change the look and feel of the website so you need to change the
theme, the custom code that you have added will no longer work since it was there in the
previous theme. On the other hand, plugins are not dependent on a specific theme, which
means that you can switch themes without losing the plugin’s functionalities. Using a plugin
instead of a theme also makes the functionality you want to create easier to maintain and it
will not be impacted during the theme updates.
Types of WordPress Plugin:
Plugins can carry out lots of tasks. It adds extra functionalities to your site which makes the
website more user-friendly.
Types of WordPress plugin include:
WordPress Security and Performance Plugins
Marketing and sales plugins for things like SEO, social media, etc
Custom content plugins such as custom post types, widgets, shortcodes, contact
forms, image galleries, etc.
API plugins that work with the WordPress REST API
Community plugins that add social networking features like the Forum feature.
3. From the Resource Library of Andolasoft.Inc | Web and Mobile App Development
Company
3
How to Run Your Plugin Code: Options
Few methods are there to activate your code in WordPress like,
functions
action and filter hooks
classes
Let’s deepdive on the above points.
Functions
Functions are the building blocks of WordPress code. They’re the easiest way to get started
writing your own plugins and the quickest to code. You’ll find plenty of them in your
themes’ files too.
Each function will have its own name, followed by braces and the code inside those braces.
The code inside your plugin won’t run unless you call the function somehow. The simplest
(but least flexible) way to do that is by directly calling the code in your theme or somewhere
else in your plugin.
Here’s an example function:To directly call that function in your theme, you’d simply type
andola_myfunction() in the place in your theme template files where you want it to run. Or
you might add it somewhere in your plugin… but you’d also need to activate the code that
calls it!
There are a few limitations to this:
If the function does something that isn’t just adding content somewhere in a theme
template file, you can’t activate it this way.
If you want to call the function in multiple places, you’ll have to call it again and
again.
It can be hard to keep track of all the places you’ve manually called a function.
It’s much better practice to call functions by attaching them to a hook.
4. From the Resource Library of Andolasoft.Inc | Web and Mobile App Development
Company
4
Action and Filter Hooks
By attaching your function to a hook, you run its code whenever that hook is fired. There are
two types of hooks: action hooks and filter hooks.
Action hooks are empty. When WordPress comes to them, it does nothing unless a function
has been hooked to that hook.
Filter hooks contain code that will run unless there is a function hooked to that hook. If
there is a function, it’ll run the code in that function instead. This means you can add default
code to your plugin but override it in another plugin, or you can write a function that
overrides the default code that’s attached to a filter hook in WordPress itself.
Hooks are fired in three ways:
By WordPress itself. The WordPress core code includes hundreds of hooks that fire at
different times. Which one you hook your function to will depend on what your function
does and when you want its code to run. You can find a list of WordPress hooks in the
developer handbook.
By your theme. Many themes include action and filter hooks that you can use to add
extra content in key places in your website’s design. And all themes will include a
wp_head and wp_footer hook. Combine these with conditional tags, and you can run
specific code on certain types of pages in your site.
By your plugin or other plugins. You might add an action hook to your plugin and then
add functions in your include files that attach code to that hook. Or you might write a
filter hook and then have a function that overrides its contents under certain
circumstances. Alternatively, if you’re creating a plugin to complement another plugin,
you can hook your functions to the existing hook in the third-party plugin.
Some of this is more advanced, but with your first plugin, you’ll probably be hooking your
functions to an action or filter hook output by WordPress itself, most likely an action hook.
Classes
Classes are a way of coding more complex features, such as widgets and customizer
elements, that make use of the existing WordPress APIs.
When you write a class in your plugin, you’ll probably be extending an existing class that’s
coded into WordPress. This way, you can make use of the code provided by the class and
tweak it to make it your own.
5. From the Resource Library of Andolasoft.Inc | Web and Mobile App Development
Company
5
An example would be the customizer, where you might write a class including a color picker,
making use of the color picker UI that’s provided in the existing class for the customizer.
Using classes is more advanced than functions, and it’s unlikely you’ll do it in your plugin.
If you do write classes, you’ll still have to use actions or filters to get them to run.
Let’s start with the basics first.
WordPress plugins are stored inside the wp-content/plugins folder which can be accessed
from WordPress root directory.
Creating a simple “Hello World” plugin in WordPress can be done in 3 easy steps:
Creating the plugin’s main folder and the plugin file
Creating plugin headers in the created plugin file (headers: information about the
plugin, version, and the author)
Writing custom functions to display “Hello World” text inside an admin page in
WordPress panel
Prerequisite
Some knowledge in basic installation & setup of WordPress, to develop custom
Plugins is necessary.
Always use the latest WordPress version available.
Coding knowledge for PHP is required.
The Plugin needs to be tested in a clean WordPress setup.
An Editor of your choice might be required.
Steps:
Enable debug mode for bug tracking. You can do so by adding ‘define(‘WP_DEBUG’,
true)’ to the ‘wp-config.php’ file.
Use wp_enqueue_style() and wp_enqueue_script() to add style sheets and scripts to
a Plugin; This prevents scripts from being loaded multiple times.
All the Plugins will be there in the wp-content > plugins folder.
Step 1: Create a New Plugin File
To start creating a new plugin, you will need access to your site’s directory. The easiest way
to do this is by using SFTP, which is a method for viewing and editing your site’s files when
it’s located on an external server.
6. From the Resource Library of Andolasoft.Inc | Web and Mobile App Development
Company
6
Create a folder andola-hello-world inside the plugins folder.
Note: Keep the name unique, so that it doesn’t conflict with other Plugins used in the
website.
The Main Plugin File
The main plugin file is essential. It will always be a PHP file, and it will always contain
commented-out text that tells WordPress about your plugin.
Create a file named andolasoft-hello-world.php where we can write our Plugin functionality
code.
<?php
/**
* Plugin Name: Andola Hello World
* Plugin URI: https://wordpress.org/plugins/
* Author: Andolasoft
* Author URI: https://www.andolasoft.com/
* License: GPLv2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Description: This is the very first plugin I ever created.
* Version: 1.0
* Text Domain: andola-hello-world
*/
You can see that the information provided in the plugin file is used to populate this entry
and provide links.
7. From the Resource Library of Andolasoft.Inc | Web and Mobile App Development
Company
7
Other information about the plugin is contained in the README.txt file, which is used to
populate the plugin’s page in the plugin directory:
This tells WordPress what your plugin does, where to find out more about it, and who
developed it. It also gives information about the version number and the text domain.
WordPress takes this information and uses it to populate the plugins screen in your site.
Here’s how it looks on that screen:
if ( ! defined( 'ABSPATH' ) ) die( 'Error!' );
add_shortcode('hello-world', 'andola_hello_world_function');
function andola_hello_world_function(){
return "Hello World! This is the very first plugin I ever created.";
}
That’s it, your plugin is ready!
Step 2: Activate your new plugin
Login to your WordPress Dashboard, go to ‘Plugins’, your “Hello World” plugin is there. All
you need to do now is activate it.
Step 3: Start using your own plugin
Create a new post and insert short-code ‘[hello_world]’ into it:
8. From the Resource Library of Andolasoft.Inc | Web and Mobile App Development
Company
8
Then this is how it will appear in the front end:
Plugin Best Practices
Before you start coding your plugin, it helps to understand best practices for plugins so your
code can be high quality right from the start.
These include:
Write your code according to WordPress coding standards. If you want to submit your
plugin to the plugin directory, you’ll have to do this.
Use comments throughout your code so other people can work with it—and so you
remember how your code works when you come back to it in the future.
Name your functions, hooks, and classes using prefixes so they are unique to your
plugin. You don’t want to give a function the same name as another function in a
different plugin or in WordPress core.
Organise your folders logically, and keep your code separated so other people can
understand it and so you can add to it over time without it becoming a mess.
You might think that using best practice isn’t necessary because it’s just you working with
the plugin. But your plugin might grow over time, you might let other people use it, or you
might sell it. Or you might come back to it in two years and not be able to remember how
the code is organized!
9. From the Resource Library of Andolasoft.Inc | Web and Mobile App Development
Company
9
FAQs
Here are the answers to some of the most frequently asked questions about WordPress
plugins.
Why can’t I just add the code I need to my theme functions file?
It’s tempting to simply keep on adding code to the functions.php file, and there is some
code that should be there.
But if your code is related to functionality in your site, rather than the design or the output
of content, then you should code it into a plugin. This means that if you switch themes in
the future, you still have that functionality. And you can use the plugin on another site
running a different theme.
I’ve added code to my plugin. Why is nothing happening?
This is probably because you haven’t hooked your code to an action or filter hook. Until you
do that, nothing will happen.
When I edit my plugin and check my site, I get a white screen. Help!
You’ve probably added some code that’s got an error in it somewhere. PHP is an unforgiving
language, and this might be as minor as a semicolon in the wrong place.
Try turning on WP_DEBUG in your wp-config.php file, and you’ll see a message telling you
where the error is. Then you can fix it.
When I activate my plugin, I get an error message telling me too many headers have been
output. What does this mean?
All this normally means is that there are too many empty lines in your plugin file. Go back
and check there are no empty lines at the beginning of the file.
10. From the Resource Library of Andolasoft.Inc | Web and Mobile App Development
Company
10
If that doesn’t fix it, try turning on WP_DEBUG.
Conclusion
Developing a custom plugin is a way to add functionality to a WordPress site that currently
available plugins don’t offer. It can be a simple plugin that implements minor alterations or
a complex one that modifies the entire site.
Are you looking to develop a custom WordPress plugin! Let Discuss