WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I see it is possible to use add_filter with the body_class function, but I'm needing to add one to the get_body_class function.

How can I add a filter to a function that is not included in the list of already available filter hooks?


share|improve this question
up vote 2 down vote accepted

The body_class() function simply calls the get_body_class() and creates a HTML class string from it.

function body_class( $class = '' ) {
    echo 'class="' . join( ' ', get_body_class( $class ) ) . '"';

So, the body_class filter applies to both functions.

share|improve this answer
Thanks for the reply. I saw that in /wp-includes/post-template.php, and this is working, but I'm confused as to why. I added add_filter('body_class', 'my_handler'); in the plugin I'm writing. When I called get_body_class in a page template, it correctly returned the filtered class string. Perhaps I'm not understanding filters correctly, but if body_class is calling get_body_class (and not the other way around), why is this filter, which is added to body_class() also applied to get_body_class()? Just curious. – robertwbradford Dec 12 '11 at 18:02

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.