fbpx

How to create a WordPress plugin?

What is WordPress plugin

WordPress plugin is a piece of code written by other developers which can help to extend WordPress website function.

For beginner it’s a best practise to start develop a plugin using WordPress Plugin Boilerplate.

Why use WordPress Plugin Boilerplate

How to create plugin using WordPress Plugin Boilerplate

For this example, we create a Page View Count plugin. Before start I need to brief some folders and files that I will use to create this plugin.

  • The files in admin folder load in backend which is wp-admin area. On the other hand, the files in public folder load in frontend.
  • The file class-page-view-count-public.php help to enqueue CSS, JavaScript and load partials.
  • The file class-page-view-count.php help to load hooks and dependencies use on frontend and backend.
  1. First, head over to https://wppb.me/
  2. Input all your plugin details and click Build Plugin. For my case, I will input page view count for plugin name.
  3. A zip file will be downloaded, the plugin is done generated.
  4. I start by adding hook to the file class-page-view-count-public.php as I need the hook to load on the frontend.

    Please take note the string ‘page-view-count’ is a variable as it depends on plugin name you input while generate plugin.
  5. Next, I add css in page-view-count-public.css. You can refer file path in function of enqueue_styles.

     

  6. After that, I add action in function of define_public_hooks in file class-page-view-count.php to make sure hook loaded on frontend.

    The first parameter for add_action would be the name of hook, second is reference to public object and the third would be the hooked function.
  7. To recall I had done add hook, add css and add action to call hooked function. Last, compress the plugin folder and upload the zip file as the way we upload new plugin in WordPress site.
  8. After upload, activate the plugin and I can see the page view count plugin in the plugin list.

    I can then place the shortcode in the website. You can follow the process as stated above, replace with your own function and see how it works.

You had learned how to create plugin and add hook to be used on frontend using WordPress Plugin Boilerplate.

Scroll to Top