How to create simple WordPress Plugin

In WordPress plugins is the PHP script that extends the functionality of the website. They can easily be downloaded and install from the WordPress Dashboard.

You can also create your own plugins and use it.

All plugins are managed in wp-content/plugins/ directory.

In this tutorial, I show how you can create a simple WordPress plugin.

Contents

  1. Create Directory and Files
  2. customplugin.php
  3. displaylist.php
  4. addentry.php
  5. Activate Plugin
  6. Conclusion

1. Create Directory and Files

Create a new directory customplugin in wp-content/plugins/ directory.

In this directory create 3 files –

  1. customplugin.php – This is the main file where define plugin details, create a new MySQL table on plugin activation, menu, and submenu.
  2. displaylist.php – List records from MySQL table.
  3. addentry.php – Add new record in MySQL table.

Create another directory img in the customplugin/ directory where store plugin icon.

2. customplugin.php

Specify Plugin name, Plugin URI, Description, Version, Author, and Author URI between /* */.

Create new table

To create new table define customplugin_table() function where create global $wpdb variable.

Set the table name with prefix and prepare CREATE TABLE query.

Include upgrade.php file and pass $sql in dbDelta() function.

Pass the customplugin_table in the register_activation_hook().

Add Menu and submenu

Define a customplugin_menu() function.

To add menu call add_menu_page() function –

I create a Custom Plugin menu.

To add submenu call add_submenu_page() function –

I create two submenus – All entries and Add new Entry.

Created two functions – displaylist() and addEntry() where include files. This function names used in add_submenu_page() method.

Call action hook where pass customplugin_menu.

Completed Code


3. displaylist.php

Fetch records from customplugin table and list data in the <table>. In the <tr> also create a Delete link where pass ?page=allentries&delid=".$id.".

On delete link pressed execute DELETE query on the $_GET['delid'].

Completed Code


4. addentry.php

Create a <form > which have 3 <input type='text' > and submit button.

On <form > submit read values and execute insert query.

Completed Code

5. Activate Plugin

  • Login to your WordPress Admin Dashboard.
  • Navigate to Plugins.
  • Find plugin and click on the Activate.

  • A new menu is been added in the Sidebar.

6. Conclusion

If you don’t need a new table for the custom plugin then remove register_activation_hook() and add menu and submenu using add_menu_page() and add_submenu_page() methods.

Free Exclusive Traffic Tips

About the Author: Dharmendra Yadav

Dharmendra Yadav is a PHP developer. Dharmendra Yadav background includes B.Tech in Computer Science and Engineering. Currently he is working with CORE PHP, CODEIGNITER, , Laravel, Opencart, JAVASCRIPT, JQUERY, AJAX, WORDPRESS, WEB API technologies , Smarty.

You May Also Like

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *