I am currently creating a plugin that allows users to generate groups of tags for their posts. Tags are terms in WordPress that help search engines find/categorize your posts and currently WordPress makes you enter or select them from a list one at a time. I watch my wife post on a daily basis and see how painful this can be. The added benefit of this plugin will be that a group of tags can change independently of a post and the post will still receive those updates.
So far, I have created the Post Creation/Modification portion of the plugin and will be moving onto the Administration portion of the plugin now. Here is the code:
/*
Plugin Name: Tag Grouping
Plugin URI: http://URI_Of_Page_Describing_Plugin_and_Updates
Description: Create and maintain groups of commonly used tags for posts. Add these groups to posts without having to add them individually.
Version: The Plugin’s Version Number, e.g.: 1.0
Author: Michael
Author URI: http://www.croutonsoflife.com
Copyright 2010 Mike of CroutonsofLife (email : mike@croutonsoflife.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
<?php
global $post_ID;
register_activation_hook(__FILE__, ‘taggroups_install’);
/* Actions */
add_action ( ‘admin_menu’, ‘render_tag_post_box’ );
add_action ( ‘pre_post_update’, ‘update_group_posts’ );
/*Filters*/
add_filter ( ‘the_content’, ‘get_postID’ );
/*********************************************************************************
Runs every time the plugin is enabled by the admin of the wordpress site.
**********************************************************************************/
function taggroups_install () {
global $wpdb;
/* Create the table that holds your Tag Group names and ID’s */
$table_name = $wpdb->prefix . "group_term_groups";
if($wpdb->get_var("SHOW TABLES LIKE ‘$table_name’") != $table_name)
{
$sql = "CREATE TABLE " . $table_name . " (
groupName TEXT NOT NULL,
groupID INT NOT NULL AUTO_INCREMENT PRIMARY KEY
);";
require_once(ABSPATH . ‘wp-admin/includes/upgrade.php’);
dbDelta($sql);
}
/* Create the table that holds your tags(terms) that are associated with groups */
$table_name = $wpdb->prefix . "group_components";
if($wpdb->get_var("SHOW TABLES LIKE ‘$table_name’") != $table_name)
{
$sql = "CREATE TABLE " . $table_name . " (
groupID INT,
termID INT
);";
dbDelta($sql);
}
/* Create the table that holds the groups that are associated with your posts */
$table_name = $wpdb->prefix . "group_posts";
if($wpdb->get_var("SHOW TABLES LIKE ‘$table_name’") != $table_name)
{
$sql = "CREATE TABLE " . $table_name . " (
groupID INT,
postID INT
);";
dbDelta($sql);
}
}
function render_tag_post_box()
{
add_meta_box( ‘taggroup_sectionID’, ‘Tag Groups’, ‘taggroups_post_box’, ‘post’, ‘side’, ‘low’);
}
/*********************************************************************************
Generates the seletion box seen by the user when creating or modifying a
Post. This box shows all groups and whether they are already enabled for
the working Post.
**********************************************************************************/
function taggroups_post_box(){
$checked_groups = NULL;
$groups = NULL;
$checked_groups = get_post_groups();
$groups = fetch_groups();
if (sizeof($checked_groups) > 0)
{
foreach ($groups as $group)
{
$found = false;
foreach ($checked_groups as $checked_group)
{
if ($checked_group->groupID == $group->groupID)
{
?>
<BR><input type="checkbox" id="<?php echo $group->groupID ?>" name="<?php echo $group->groupName ?>" value="1" checked />
<?php
echo $group->groupName;
$found = true;
}
}
if ($found <> true){
?>
<BR><input type="checkbox" id="<?php echo $group->groupID ?>" name="<?php echo $group->groupName ?>" value="1" />
<?php
echo $group->groupName;
}
}
}
?><BR><BR><?php
echo "Enable the groups that contain tags you want to include with your post.";
}
/*********************************************************************************
Query table wp_group_term_groups and get all groups.
Returns an array of groups.
**********************************************************************************/
function fetch_groups(){
global $wpdb;
$table_name = $wpdb->prefix . "group_term_groups";
$groups = $wpdb->get_results("SELECT * FROM $table_name");
return $groups;
}
/*********************************************************************************
Query table wp_group_components and get all tags associated with a group.
**********************************************************************************/
function fetch_tags($groupID){
global $wpdb;
$table_name = $wpdb->prefix . "group_components";
$comments = $wpdb->get_results("SELECT * FROM $table_name WHERE $groupID = groupID");
return $comments;
}
/*********************************************************************************
Loop through and find all group checkboxes that are enabled.
Call write_group_posts to store these selections in the database.
**********************************************************************************/
function update_group_posts(){
clear_group_posts();
$groups = fetch_groups();
foreach ($groups as $group){
if(isset($_POST[$group->groupName])){
write_group_posts($group->groupID);
}
}
}
/*********************************************************************************
Prior to calling write_group_posts, this function should be called to
clear the groups that are associated with the working post. If it is not
called, there is a possibility that groups will be associated multiple
times per post.
**********************************************************************************/
function clear_group_posts(){
global $wpdb;
global $post_ID;
$table_name = $wpdb->prefix . "group_posts";
$sql = "DELETE FROM " . $table_name . " WHERE postID = $post_ID";
$results = $wpdb->query($sql);
}
/*********************************************************************************
Updates the database table wp_group_posts with all groups associated with
the working post.
**********************************************************************************/
function write_group_posts($groupID){
global $wpdb;
global $post_ID;
$table_name = $wpdb->prefix . "group_posts";
$sql = "INSERT INTO " . $table_name . " VALUES ($groupID, $post_ID)";
$results = $wpdb->query($sql);
}
/*********************************************************************************
Stores the postID of the post the user is working on as a global variable
from inside the WordPress loop for use later, when outside the loop.
**********************************************************************************/
function get_postID(){
global $post;
global $post_ID;
$post_ID = $post->ID;
}
/*********************************************************************************
Retrieve all groups that have been associated with a post.
Returns an array of results.
**********************************************************************************/
function get_post_groups(){
global $wpdb;
global $post_ID;
$table_name = $wpdb->prefix . "group_posts";
$sql = "SELECT groupID FROM " . $table_name . " WHERE postID = $post_ID";
$results = $wpdb->get_results($sql);
return $results;
}
?>
I will go into more detail in another, later post. If you have any questions or suggestions, please feel free to email me!
Related posts: