Learn Plugin Development with a Bulk Category Creator

In this tutorial, you will begin to learn plugin development for WordPress. The example plugin will have an admin page with a basic form element and a submit button, and will perform the simple task of creating bulk categories in one go.

A lot of times, when developing a website or blog on WordPress, you would need to create a lot of categories. In these instances, it can become a pain to create categories one by one. So, in this tutorial you will learn to build a plugin which allows a list of comma separated categories to be copy pasted into a text area. And on clicking a button, all the categories are loaded into the WordPress categories section in a single go. So, this will prevent the need to sit and click on the Add New Category button for every category that has to be created.

Requirements:

  1. You will need a local installation of WordPress (or a development server of some kind)
  2. You will need to know basic HTML and PHP

What are you going to build (Preview of the final stage of what you are going to create)

Bulk Category Creator

A Brief Note About WordPress Plugins

Plugins in WordPress are basically PHP scripts which allow you to change or add new features to your existing WordPress installation. It could be something really simple, like the “Hello Dolly” plugin, or something really advanced like the Jetpack or Google Analytics plugins.

So, why should you build a plugin?

Most WordPress developers are aware that they should not hack WordPress core files to suit their needs. It creates updating issues, and could be insecure. But if altering WordPress core code is not a good option, how else will you add new features to your WordPress website? The answer, of course, is via plugins. An added benefit to learning how to create plugins is that you get to learn more about WordPress itself, and how it works. That in turn will make you a better WordPress Developer.

Learning Plugin Development

In order to create a plugin, you have to create a particular folder structure for your plugin. Every plugin has a directory name and one or more PHP files inside it.

Inside your WordPress directory structure, you will find a directory called wp-content. Inside the wp-content will be a directory called plugins.

Your folder structure would look similar to this:

File Structure

First Steps – Creating Your Files

The very first step in creating a plugin is to navigate to the wp-content directory of your WordPress installation and inside the directory navigate to the plugins directory. Inside the plugins directory create a new directory called bulk-category-creator.

Once, you have created the directory, create a file named bulk-category-creator.php. Make sure that the directory name and the PHP file name are the same.

Now, open your text editor and copy paste the following code into it

<?php
   /*
   Plugin Name: Bulk Category Creator
   Plugin URI: http://www.ruforaweb.com
   Description: A plugin to bulk-create categories in one go
   Version: 1.0
   Author: Vishnu Ajit
   Author URI: http://twitter.com/vishnuajith310
   License: GPL2
   */

Every plugin has a section of code comments at the top containing a description, version information, author name and licensing information. This is usually found in the header of the plugin’s main file. In our case, it is found in bulk-category-creator.php.
This section is the metadata of the plugin. Let us dissect the example metadata line by line.

  • Plugin Name: Here is the name given to the plugin, in our case we call it the Bulk Category Creator
  • Plugin URI: This is a link to where the plugin can be found on the internet for download purposes. It can be found in the WordPress.org archives or in another location.
  • Description: This is the section where you can describe what actions the plugin performs.
  • Version: Specifies the version of the plugin
  • Author: Name of the author or developer who created this plugin
  • Author URI: Link to the author’s address on the internet
  • License: The license under which this plugin is released

Navigate to your Admin Dashboard > Plugins > Installed Plugins and you should see your plugin in the list.

Code Layout – Bulk Category Creator Plugin

Before jumping into coding our plugin, let us understand some basic WordPress hooks.
Basically, in WordPress there are two main types of hooks: Actions and Filters. Since, this is a basic plugin development tutorial, we shall not go into too much detail, but basically this is how they differ.

  • Actions: Actions are used when you add something when an event has occurred. Example: an action to tweet an article when a new post has been published.
  • Filters: Filters are used when you have to change some data or process it.

Now that you are familiar with the basics, let us start coding our plugin.

add_action('admin_menu', 'rfr_CategoryCreatorMenu');

This line of code hooks the admin_menu to the function rfr_CategoryCreatorMenu

The admin_menu function is used to add submenu or menu options to our WordPress Admin Dashboard. Once that has been done, write down the next snippet of code.

Continue reading %Learn Plugin Development with a Bulk Category Creator%


Source: Sitepoint