If you have a WordPress site you will soon jump into the pool of plugins and install one after the other for your own purposes. But also the time will come when you will want to customize something, make a small adjustment, solve a little problem for which you will find no good plugins. And you will wonder: is it possible to solve this little problem on my own?
The good news is: you can! Even with a very little programming background you can create your own WordPress plugins and we will show you how!
What do you need to create your very own WordPress plugin?
- some basic knowledge of programming in general (functions, loops, variables etc.)
- any text editor to begin with (we recommend Notepad++)
- you should know how to upload files to the ftp of your homepage (use Total Commander, or any online tool you find in cPanel)
That’s it. What are some other areas that will give you a huge advantage:
- some PHP knowledge
- SQL and general database knowledge
So let’s see how to create the Hello World plugin for your WordPress site in 5 minutes or less!
Step 1# – Create the plugin folder and file
On your local machine create a folder called ‘my-first-plugin’. In this folder create a file called ‘my-first-plugin.php’.
Add below code into the ‘my-first-plugin.php’ file:
That’s it, your plugin is ready!
Step #2 – Upload and activate your new plugin
On the server of your WordPress site locate folder /wp-content/plugins/, you can navigate to it from your home. Copy your ‘my-first-plugin’ folder to here:
Login to your WordPress Dashboard, go to ‘Plugins’, and voilá, your brand new plugin is there. All you need to do now is activate it.
Now your plugin is ready and active!
Step #3 – Start using your own plugin
Create a new post and insert shortcode ‘[hello_world]’ into it:
Hit ‘Publish’ and open your new post in your browser!
As we have Social Pug installed, it can instantly be shared on Facebook, Twitter and Pinterest! 🙂
Let’s have a second look at our new plugin
As you can see, the plugin starts with a comment section that has the Plugin Name. You can add further info here, but this is the bare minimum, this phrase is what you see on your WP Dashboard under Plugins.
Function ‘add_shortcode’ will connect the shortcode [hello_world] that you use in your posts to a custom function that you write, ‘hello_world_function’ in this case.
! Make sure your function names are very unique, not to cause any interference with other plugins!
In our function we have only one line which is a return statement. You could also use the line
echo “Hello World”;
and it would output the same text. So what is the difference? If you use echo, your text will appear on the page, but you can’t really be sure about it’s location! But if you use the return statement, your text will appear exactly where your shortcode is within your post!
How to write a Google Ads plugin in 2 minutes?
Now that you know how to create a Hello World plugin, let’s see another use case from real world. You wish to show Google ads in your posts but don’t want to copy-and-paste all that script several times.
The solution? It couldn’t be easier! Just create one more shortcode in your plugin for displaying Google ads, like:
Once you upload this updated file, all you have to do is insert [show_google_ads] into your posts and it will show Google ads! Easy as that!
Final words for creating simple WP plugins
When creating shortcodes, you may want to add these in an init hook:
Observe how the code changed. We have added an init hook at line 6 and our shortcodes are added within this hook. What is an init hook all about? According to WordPress development guide init hook “Fires after WordPress has finished loading but before any headers are sent.”
You may want to use this advantage in more complex tasks.
How to go further from here? I think you already have a lot of ideas racing in your mind!
Just imagine how much you can do with php! For example you can easily access the SQL database which comes with WordPress. You can have your own tables or access tables of WordPress or, by the way, the database tables of any other plugins!
Next we will see how you can add parameters to your shortcodes and use parameters in your URLs.