So you want to create your first WordPress plugin. Well, you’re in the right place. A plugin can be as simple or as complex as you need it to be. You can use it to create a plugin that displays your main header, or lists all of your users or one that does some absolutely amazing customizations to your theme. The sky is the limit, it just depends on what you intend to use the plugin for. If you are already use to writing functions on your theme pages, functions.php or in external component files, you can easily do the same thing but instead place that code inside of a plugin for better organization.
Let’s first look at the basics for what you need to create a plugin, and then you can expand from there.
The Basics
The first thing you’ll need when creating your plugin is a name for your plugin. For the purpose of this example, we’ll create a plugin that displays “Hello World” after your header. We’ll call it “display_hello_world”. You’ll need one folder and one file for this plugin:
- Folder: display_hello_world
- File: display_hello_world.php
Note: You don’t have to actually create a folder to put the file in, but it’s good practice to do so.
After creating your folder and php file (inside of the folder) for your plugin, add the following code to the top of the display_hello_world.php file:
<?php
/* Plugin Name: Display Hello World Plugin */
It may surprise you to know that this is all you need to create and activate your custom plugin. Now make no mistake, what we have doesn’t actually do anything, but technically it is a plugin that can be uploaded to the wp-content/plugins/ directory like any other plugin. There are tons more options you can add to the header when creating your plugin, but as a barebones example that is all you’d need. To see more options, please visit this link.
So now we can add this small snippet of code to display the words “Hello World” after your header. Inside of your display_hello_world.php file, add the following after the plugin header text we added:
function display_hello_world() {
echo “<p>Hello World</p>”;
}
add_action(‘genesis_after_header’, ‘display_hello_world’);
and then save the file.
That’s it. Test it out and then expand from there.
Leave a Reply