«PREVIOUS 1 2 3 4 View All NEXT»
Step 3. Finishing Touches…
In this step, we will cover the following:
- 1. SAVE: Saving the Information to the Database
- 2. DELETE: Delete Record, Upon Deletion of Post
- 3. DISPLAY: Display the Icon for Each Post Content
Within our content-icons.php file, at the end of the last code block that we entered, copy and paste the following code block:
-
/**
-
** Function: Save our information whenever we save or publish the post
-
**/
-
add_action('save_post', 'add_cicons_admin_process');
-
function add_cicons_admin_process($post_ID) {
-
global $wpdb;
-
-
$cPID = wp_is_post_revision($post_ID);
-
if($cPID != null){
-
$select_icon = $_POST['select_icon'];
-
// Ensure No Duplicate Field
-
$check = intval($wpdb->get_var("SELECT * FROM `$wpdb->cicons` WHERE `post_id` = $cPID"));
-
if($check == 0) {
-
$wpdb->query( "INSERT INTO $wpdb->cicons (post_id, icon_name) VALUES ($cPID, '".$select_icon."')" );
-
} else {
-
if($select_icon != null){
-
$wpdb->query( "UPDATE $wpdb->cicons SET icon_name = '".$select_icon."' WHERE post_id = $cPID" );
-
}
-
}
-
}
-
}
-
-
/**
-
** Function: Delete the content icon associated with the post when the post itself gets deleted
-
**/
-
add_action('delete_post', 'delete_cicons_process');
-
function delete_cicons_process($post_ID) {
-
global $wpdb;
-
-
$cPID = wp_is_post_revision($post_ID);
-
if($cPID != null){
-
-
// Only proceed further if the content icon exist
-
$check = intval($wpdb->get_var("SELECT * FROM `$wpdb->cicons` WHERE `post_id` = $cPID"));
-
if($check > 0) {
-
$wpdb->query( "DELETE FROM $wpdb->cicons WHERE post_id = $cPID" );
-
}
-
}
-
}
-
-
/**
-
** Function: Display our content icon within the post/the content itself
-
**/
-
function display_content_icon($post_content){
-
global $wpdb, $post;
-
if($post->ID != null){
-
// Only proceed further if the content icon exist for the post
-
$chkIfExist = $wpdb->get_results("SELECT icon_name FROM `$wpdb->cicons` WHERE `post_id` = $post->ID");
-
if($chkIfExist) {
-
$imageStr = '<img id="content_icon" src="'. cicons_url().''.get_upload_path().'/'.$chkIfExist[0]->icon_name.'" align="right" style="padding: 5px" />';
-
$post_content = $imageStr . $post_content;
-
}
-
}
-
return $post_content;
-
}
-
//we need to tell word press to pass the content THROUGH our function… so we can append content icon to the post
-
add_filter('the_content', 'display_content_icon');
Explanation:
On line 170, we are telling wordpress, “Look brother, whenever a user of this system tries to save/update the post, include my function as well…. please“. That being said, The function that follows, is the one that will be invoked when wordpress is saving a post (lines 171 - 187).
The most important line in this block, or so I feel that way, is line 173: This line basically employs WordPress’s built in function to get the ACTUAL PostID as oppose to the ridiculous and unimportant RevisionID. This is important because otherwise, we would be making absurd number of database inserts/updates queries (especially if the author is like me, who writes a long post, and in the background WordPress keeps creating auto-save entries…)
Similarly, On line 192, we are telling wordpress to call our function when a user tries to delete a post. The function that follows (lines 193 - 205) basically checks to see if we have a database table entry containing the current post’s ID (ID of the post that is to be deleted) as one of the records. If it does, the function deletes the record. We don’t need it, once the post is deleted… so why keep the record around?
Finally, On line 223, we are telling wordpress that whenever it is showing content, pass the whole content through our function (lines 210 - 221), so that we can add the icon that is associated with a particular post.
And there you have it! Save the file, upload/overwrite existing content-icons.php file, activate it and start playing around with it. Remeber, upload all your icons inside the “ROOT/wp-content/uploads/cicons/” folder! (this folder should be created for you once you activated the Plugin from the Admin area)
Furthermore, I have not walked-through each line of code in this Guide, as I feel that I have done enough explanation after each code block AS WELL AS in-line commenting. However, if you are unsure of something, please do ask.
Questions? Comments? Critiques? Features Request
Please direct all your questions/comments/critiques/feature requests at the CodingRush.com site–which is the official site for this Plugin, as also the official site of all my future works. GuidesForge is simply just that: a site that hosts (mainly) my tutorials, how-tos, and step-by-step guides (among other services).
Suggestions etc. For This Step-by-Step Guide
However, if you have any suggestions/improvements/ or would like to add to the explanation of the code or this guide in general, than please use the comments section below.
Finally:
I hope you enjoyed this step-by-step guide as much as I did writing it! (Not to mention all the frustration that I went through in order to make this Plugin). Really though, you must write one yourself in order to get the true joy! So, if you believe in assignments, your next assignment is to write a plugin (even if its something that has already been done before)… it could be a simple one, or you can just go wild and run with an idea… whatever the case maybe, have fun doing it, as that’s all it matters.
Credits:
No work would be properly “complete” until/unless the credits are given where they are due. So here we go–
First and Foremost, the Author of WP Post-Icon, Linewbie. Next to the Author of WP-Sticky, Mr. Lester Chan and finally to the Author of Category Icons, Mr. Sadish Bala for help with coding. (I used their plugins’ code as a reference from point-to-point)
Enjoy!
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