«PREVIOUS 1 2 3 4 View All NEXT»
Step 1. Activating the Plugin
In this step, we will cover the following:
- 1. Creating Required Tables
- 2. Creating (recursively) Directory Structure to Hold our Content Icons
- 3. Adding a menu entry to the Admin Menu
- 4. Adding a “Meta Box” on the Post Page for Associating/Selecting Icons
Before we begin though, add the following block of code, right after the Warranty comment block:
-
//Define table name to use throughout our Plugin
-
global $wpdb;
-
$wpdb->cicons = $wpdb->prefix.'ci_post_ref';
-
-
//Define a term that points to the uploads folder
-
define('CICONS', ABSPATH."/wp-content/uploads/cicons");
Explanation:
The first two lines define a variable that is globally accessible for help with plugin development. As you will see, we make extensive use of this variable through out our plugin development. The next two lines deal with defining a term that points to our uploads folder–where all our images will reside.
With the content-icons.php file open, add the following code block. I am leaving the comments inside for your study.
-
/**
-
** Function: Adding "Content Icons" Meta box
-
**/
-
add_action('admin_menu', 'cicons_add_meta_box');
-
function cicons_add_meta_box() {
-
add_meta_box('ciconsdiv', __('Content Icons', 'cicons'), 'cicons_metabox_admin', 'post', 'side');
-
}
-
-
/**
-
** Function: Adding "Content Icons" menu entry to the Options Menu
-
** We are commenting out this for now, as we don't need a menu option…
-
add_action('admin_menu', 'cicons_menu');
-
function cicons_menu() {
-
if (function_exists('add_options_page')) {
-
add_options_page(__('Content Icons', 'wp-cicons'), __('Content Icons', 'wp-cicons'), 'manage_options', 'content-icons/content-icons-options.php');
-
}
-
}*/
-
-
/**
-
** Function: Content Icons Activate & Install
-
**/
-
//First, we add the action, which tells WordPress to set the activation hook for a plugin
-
//this particular call requires to parameters: the file location and name and the function name
-
//add_action('activate_content-icons/content-icons.php', 'conticons_init');
-
add_action('init', 'cicons_init');
-
function cicons_init(){
-
global $wpdb;
-
-
//let's start by defining our table name
-
$table_name = $wpdb->prefix . "ci_post_ref";
-
-
//now, we make sure that such a table doesn't already exist.
-
//if our desired table name already exist, we will not Create it again!
-
if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
-
//we will be creating a table that holds two key pieces of information: icon id, and the post it belongs to.
-
$create_cicons_info = "CREATE TABLE " . $table_name . " (
-
`post_id` INT NOT NULL ,
-
`icon_name` TEXT NOT NULL ,
-
PRIMARY KEY ( `post_id` )
-
);";
-
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
-
dbDelta($create_cicons_info);
-
}
-
-
//create the upload directory
-
wp_mkdir_p(ABSPATH."/wp-content/uploads/cicons");
-
}
Explanation:
Lines 38 - 41, overall, we are adding meta box to the write post page. More specifically though: On line 40, we are using the add_meta_box WordPress Function–which was introduced in WordPress v2.5–to add a section to the Write Post page.
Lines 46 - 51, achieve the task of adding a menu entry of Content Icons. However, as mentioned in the code, we are going to comment it out for now as we really don’t need a menu entry for this Plugin just yet…. I have kept this code as-is in the above code block for two reasons:
- 1. Show you how to add menu entries and
- 2. Save me some typing for future version
![]()
In the next block, we are using dbDelta function of WordPress in order to create the required table where we will store our information.
Lastly, On line 80, we are using yet another WordPress built-in function to create our upload directory structure. This is a very useful function as it simply takes the desired path and recursively creates the structure.
Well, there you have it. We are all done with Step 1. However, do not try and activate the plugin just yet… as WordPress would throw a fit (and rightfully so, as we have yet to define a function cicons_metabox_admin() that we have called when we created the meta box!… that is saved for the next step.)
Popularity: 100% [?]
Posted by
Under
Tags: 
5 Comments Received
January 22nd, 2009 @5:38 am
Hi! A good forum, glad to join you
October 22nd, 2009 @2:29 am
Hi i read your code. It is very understandable way. But am getting error after activating this plugin.
Error: we cannot change the headers……
April 4th, 2010 @8:21 am
Hi Madhu,
Please provide more information. What version of WP are you running? Which file is it mentioning? Can you quote the error (Copy & Paste) ?
Pingback & Trackback
Leave A Reply