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?



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.

  • 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. Dec 12 '11 at 18:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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