A Complete Guide of Creating WordPress Plugin from Scratch

Parth Shah
Techcompose
Published in
2 min readOct 11, 2018

WordPress plugins are the set of functionalities/codes that can be easily active/deactivate your website without removing your code. Creating a WordPress plugin becomes a more important part in WordPress development. It’s always better to create a plugin instead of writing your code into theme files.

The advantages of writing code in plugin over a functions.php file

Its many reasons for creating a plugin instead of writing code into a functions.php file. The functionalities created in functions.php file quite difficult to understand for other developers, as well other developers can’t understand the flow of assets like what css, js, and files used for functionalities.

Pros of a plugin:

  • Code reusability is the best cons of a plugin, you can add your plugin to any of WordPress website.
  • Your functionalities will not depend on any theme, the code will work when you change a theme.
  • Easy to disable, update, or reinstall as needed
  • Easy to manage code
  • Easy to understand for the user by what each plugin does from its description

Here are steps you should follow while creating a plugin.

  1. Basic Structure:
  • Put your plugin files into the wp-content/plugins folder and create a plugin folder for your files.
  • The folder and the file must have the same name with the exception of the file extension. E.g folder name is custom_plugin, use custom_plugin.php accordingly directory named.

2. Plugin Header:

3. File Structure:

  • Use assets folder for your custom CSS, js, fonts and image files.
  • Use all other PHP files into inc/include folder

4. Keep Plugin Clean:

Keep your plugin clean by using hooks, Activate, Deactivate, and Uninstall hooks

  • register_activation_hook — it Runs when your plugin is activated
  • register_deactivation_hook — it Runs when your plugin is deactivated
  • register_uninstall_hook — it Runs when your plugin is uninstalled

a) register_activation_hook:

When a plugin is activated, the action activate_PLUGIN_NAME hook is called. With the name of this hook, PLUGINNAME is replaced with the name of the plugin, including the optional subdirectory.

b) register_deactivation_hook:

Deactivation hooks run when a user has deactivated a plugin, but before it is deleted.

c) register_uninstall_hook:

Uninstall hook is run when the user uninstalls/delete the plugin. There are two methods to perform uninstall actions once is you can write code in register_uninstall_hook and another is using a uninstall.php file in your plugin.

If you are creating uninstall.php file:

WordPress enables you to perform different customize tasks to achieve your desired functionality with an impressive layout. Enhance your online presence today with one of the reputed WordPress Development Company. Contact us to discuss your project or Hire Dedicated WordPress Developer. You can also reach us at inquiry@techcompose.com

--

--