888888.888888.88""Yb..dP"Yb..8888b..Yb..dP.88b.88....db....8b....d8.88..dP""b8..dP"Y8
88__...88__...88__dP.dP...Yb..8I..Yb.YbdP..88Yb88...dPYb...88b..d88.88.dP...`".`Ybo."
88""...88""...88"Yb..Yb...dP..8I..dY..8P...88.Y88..dP__Yb..88YbdP88.88.Yb......o.`Y8b
88.....888888.88..Yb..YbodP..8888Y"..dP....88..Y8.dP""""Yb.88.YY.88.88..YboodP.8bodP'


88b.88.888888.888888.Yb........dP.dP"Yb..88""Yb.88..dP
88Yb88.88__.....88....Yb..db..dP.dP...Yb.88__dP.88odP.
88.Y88.88"".....88.....YbdPYbdP..Yb...dP.88"Yb..88"Yb.
88..Y8.888888...88......YP..YP....YbodP..88..Yb.88..Yb

February 28, 2010

A couple of days ago I was trying to add classes to comments using WordPress 2.7+ filter comment_class. I ran into a little confusion which I thought could tie someone up and though I should share it.

To start with here is how it works:

add_filter( “comment_class” , “add_class” , 10 , 4 );

function add_class(  $classes, $class, $comment_id, $page_id){

//get the comment object
$_comment = get_comment($comment_id);

//do stuff with comment

//add our new class
$classes[] = “newclass”;

//send the array back
return $classes;

}

This is all simple enough, and works without issue. But it isn’t doing quite what it appears to be doing, and this is where the confusion comes in.

When the filter is actually run it does indeed pass those four variables ($classes, $class, $comment_id, $page_id). However, in the function that calls the filter, and the function that calls that function, the comment_id and page_id are optional. They set themselves to Null if a value isn’t given. When this whole chain begins the comment_id and page_id are not actually passed to any of those functions, so although the filter is sending them, their value is Null.

In the example above that isn’t a problem because get_comment checks to see if the comment_id passed was empty and if so goes off and grabs the comment from a global variable. So it does return the right comment. But if you wanted to use the comment_id or the page_id to grab a value that you have saved somewhere else (in one of your own options, or perhaps you are getting a custom field) this could confuse matters. It won’t work.

So, to sum it all up. If you want the comment_id or the page_id you need to get the comment, as shown above, and then get those values from the comment. You cannot rely on the filter passing those values.

Post Image The Easy Peasy Way
Wp display all post images?
26 comments
page 1065
Why I Ditched Disqus
Add wordpress comments to friendfeed?
5 comments
page 1175
Html 5 Gallery
Html5 gallery?
6 comments
page 1305
Html 5 Gallery
Html5 gallery?
6 comments
page 1305
Quick N Dirty Post Exclusion
Fitler theloop addfilter?
11 comments
page 124
Using Your Own Url Shortener
Constructor and link shortner?
4 comments
page 1190
My Experience Of Flexx
Flexx of genesis wp theme?
4 comments
page 1026
Html 5 Gallery
Html5?
6 comments
page 1305
Wordpress Chat
Tchat page wordpress?
3 comments
page 1308
Quick N Dirty Post Exclusion
Filter wordpress posts plugin?
11 comments
page 124
Post Image The Easy Peasy Way
Wordpress attach images to post?
26 comments
page 1065
Post Image The Easy Peasy Way
Wp post orderby?
26 comments
page 1065
Auto Cycle Fun With Sidebar Tabs
Wordpress make your plugin updateable?
one comment
page 1129
Post Image The Easy Peasy Way
Wp add attachment?
26 comments
page 1065
Html 5 Gallery
Html 5 gallery?
6 comments
page 1305
Html 5 Gallery
Pictures html5?
6 comments
page 1305
Html 5 Gallery
Pictures html5?
6 comments
page 1305
Wordpress Chat
Postimagetheeasypeasyway?
3 comments
page 1308
Using Your Own Url Shortener
How to flushrewriterules?
4 comments
page 1190
Custom Hooks For Admin Pages
Addaction adminpost?
one comment
page 430
Custom Hooks For Admin Pages
Addaction adminpost?
one comment
page 430
Wordpress Chat
Chat themes?
3 comments
page 1308
Html 5 Gallery
Html5 wordpress?
6 comments
page 1305
Html 5 Gallery
Html5 photo gallery?
6 comments
page 1305
How To Add Sidebars To A Theme
Add more than one widget area to wordpress?
10 comments
page 1053
Post Image The Easy Peasy Way
Wordpress postimage img tag?
26 comments
page 1065
Why I Ditched Disqus
Disqus themes?
5 comments
page 1175
Html 5 Gallery
Photo gallery in html 5?
6 comments
page 1305
Quick N Dirty Admin Login Screen
Admin login?
no comment
page 128
My Experience Of Flexx
Ithemes flexx theme?
4 comments
page 1026
3 Ways To Speed Up Your Blog Without A Cache Plugin
Wp plugin speedup page?
one comment
page 1321
Wordpress Chat
Wordpress chat?
3 comments
page 1308
Theming Habari Vs Wordpress
Habari vs wordpress?
13 comments
page 440
  1 query every 1232 seconds, updated 1 seconds ago.
Post a comment?