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.

Theming Habari Vs Wordpress
Habari vs wordpress?
13 comments
page 440
Post Image The Easy Peasy Way
Get post attacments?
26 comments
page 1065
Are Child Themes The Best Option
Wp child themes?
15 comments
page 1262
Upload From Url
Upload to url?
6 comments
page 326
Silence Is Golden
Silence is golden wordpress?
3 comments
page 213
How To Add Sidebars To A Theme
Html hot add a side bar?
11 comments
page 1053
How To Add Sidebars To A Theme
How to add picture to the wordpress sidebar on sidebarphp?
11 comments
page 1053
Fun With Sidebar Tabs Styling
Position of the tabs thematic?
2 comments
page 336
Using Your Own Url Shortener
Short url?
4 comments
page 1190
Why I Ditched Disqus
Styling disqus widgets?
5 comments
page 1175
Wordpress 25 Exif Fields
Wordpress exif?
12 comments
page 230
Html 5 Gallery
Html 5 gallery?
6 comments
page 1305
Dont Mess With My Toot Toot
Fun with toots?
16 comments
page 599
Dont Mess With My Toot Toot
Fun with toots?
16 comments
page 599
Converting Wordpress Themes To Habari
Habari timthumb?
one comment
page 694
Wordpress 25 Exif Fields
Habari timthumb?
12 comments
page 230
Fun With Sidebar Tabs Styling
Tabs with html css on same page javascript?
2 comments
page 336
Using Your Own Url Shortener
How to have own url short?
4 comments
page 1190
Post Image The Easy Peasy Way
Insert conditional image php wp?
26 comments
page 1065
Using Your Own Url Shortener
Run short url using htaccess?
4 comments
page 1190
Dont Mess With My Toot Toot
New posttype not displayed wordpress?
16 comments
page 599
Quick N Dirty Replacement Text
Wpupdatepost object?
no comment
page 122
How To Add Sidebars To A Theme
Wordpress sidebar above main sidebars?
11 comments
page 1053
Html 5 Gallery
Html5 simple wordpress theme?
6 comments
page 1305
Updating Code Snippets Here
Fun wordpress plugin?
one comment
page 1338
How To Add Sidebars To A Theme
Wp register sidebars?
11 comments
page 1053
Html 5 Gallery
Html5 wordpress theme?
6 comments
page 1305
Post Image The Easy Peasy Way
Get post images?
26 comments
page 1065
Updating Code Snippets Here
Wordpress fun plugins?
one comment
page 1338
Post Image The Easy Peasy Way
Wordpress get first image large?
26 comments
page 1065
Using Wordpress As A Php Framework
Wordpress create your own framework?
2 comments
page 335
Improve Your Typography With Plugins
Wordpress typography plugin?
one comment
page 721
Quick N Dirty Replacement Text
Dirty replacement?
no comment
page 122
  1 query every 1922 seconds, updated 1 seconds ago.
Post a comment?