In this tutorial, we will teach you how to create a plugin in WordPress from scratch in a very straightforward way. Creating a plugin in WordPress is not that much tough. Are you eager? Let’s just jump into it.
Points that You have to Keep in Mind While Creating a WordPress Plugin (Recommended)
We have listed our plugin in WordPress.org repository so all the following points are based on that real experience.
- The first and most important thing is that each & everything that you are going to develop should be unique like plugin name, functions, variables, file names, etc. We are suggesting you to use a prefix for all the names like scratchcode_, sc_, scratch_. You can make your own based on your company name, plugin name, etc.
- If you are planning to deploy your plugin on WordPress.org then first check the plugin name availability.
- Always secure user input before saving it into the database. It is called Data Sanitization/Escaping in WordPress. WordPress.org will not accept your plugin without this.
- Offload all remote(from another server) Images, CSS, JS, files to your plugin. Considered this point only if you want to deploy your plugin to WordPress.org plugin repository.
- Do not hard code WordPress’s database table prefix. WordPress provides the best way to use a database table prefix. You can use
$wpdb->prefix
to get database table prefix. - You should follow the coding standards up to the mark. It will help you to create a secure and qualitative product. You can visit this link for WordPress coding standards.
- You can create new database tables as much as you want but It is good to use WordPress’s existing database table instead of creating a new one.
- Don’t write, direct SQL queries, it might create a security breach. Use WordPress’s inbuilt functions for SQL queries like
wp_query()
,get_posts()
etc. - Add
if ( ! defined( 'ABSPATH' ) ) exit;
line at the top of each PHP file for security. - You must use plugin_dir_path() and plugins_url() for absolute paths and URLs within your PHP code
- Always set WP_DEBUG true from your wp-config.php file until the development is over. It will help you to eliminate basic PHP errors. Moreover, You can also add
define('WP_DEBUG_LOG', true);
into your wp-config.php file to enable a debug log. It will only work when WP_DEBUG set to true. WP_DEBUG_LOG will create a log file in the wp-content directory when any error occurs. - Last but not least, always use
wp_enqueue_style()
andwp_enqueue_script()
functions to include styles and scripts.
There are still many things that you need to keep in mind, but for beginners this is enough. We hope you read all the above points very carefully. Let’s go ahead and build a plugin.
What We Are Going to Build?
Instead of creating a very long and complex plugin, Let’s just start with a very simple concept. Here, we are going to build a simple plugin to dynamic copyright year which can be useful for the footer and other places.
Then we will build something that covers useful topics of the plugin. Are you ready to build a copyright year plugin? Let’s just dive into it.
Step by Step WordPress Plugin Creation
- Create a unique folder into the
wp-content/plugins
directory. This will be your main plugin folder which represents your product.
- Now, Create a PHP file the same as the directory name. This will be the main plugin file of your plugin from where each PHP commands will be executed. You can use any file name but generally, developers keep the same name as the directory name for the main plugin file.
- Open the file in a text editor, and paste the following information in it
<?php
/*
* Plugin Name: Dynamic Copyright Year
* Plugin URI: http://www.wordpress.org/dynamic-copyright-year
* Description: Display dynamic copyright year using shortcode
* Version: 1.0
* Author: ScratchCode
* Author URI: https://www.scratchcode.io
* License: GPL3
*/
On the above code, only Plugin Name
is required, and except its are optional. You should try to remove other attributes and play with it, but after reading the below lines.
That’s it! We’ve just completed the minimum number of steps that are required to create a WordPress plugin. You can now go to the plugins->installed plugins
and activate our newly created plugin. Hurray! Drum the roll!
What Now?
At this point, you might be thinking that what we have created and what it will do? Well, it doesn’t do anything! But let’s go ahead, add something useful so that it behaves like a plugin.
Creating Dynamic Copyright Year Plugin
Let’s now add the simple shortcode to get the dynamic year using PHP’s date() function. Copy and paste the following code into your main plugin’s file (in our case, it’s sc-dynamic-copyright-year.php
) below the plugin information and save it.
function dynamic_copyright_year(){
return date('Y'); // Return current year
}
add_shortcode('copyright_year', 'dynamic_copyright_year');
That’s it. Now add shortcode [copyright_year]
anywhere into your page or post’s editor and it will print the current year. So you can use this shortcode into your footer to print the dynamic copyright year as below or Read our guide on How to Use Shortcode in WordPress for better understandings.
Copyright © [copyright_year] Your Company Name. All rights reserved.
That’s all for the day. We hope this article helped you to create your first WordPress plugin.
Please let us know in the comments if everything worked as expected, your issues, or any questions. If you think this article saved your time & money, please do comment, share, like & subscribe. Thank you in advance. Keep Smiling! ๐ and Happy Coding!
I cherished up to you will receive carried out right here. Reina Allyn Moritz
Hello Dear, are you actually visiting this website daily, if so then you will absolutely take nice experience. Shirlene Rogerio Phionna