I want to remove a body class based on a page template, but I want the control to be per template. My solution was create a function that removes the body class, then place that inside a function that can be called in a template.

function prefix_full_width() {
    add_filter('body_class', 'prefix_remove_body_class', 10, 1);

function prefix_remove_body_class($classes){
    if (! $classes) return;
    // Remove class from array
    foreach ($classes as $key => $value) {
        if ($value == 'sidebar-primary') unset($classes[$key]);

    return $classes;

I'm expecting that running prefix_full_width() on a page template should then invoke the filter and remove the body class. Why doesn't this work? I suspect this is because the body classes are applied before the template is rendered, but I'm not sure how to test that.

  • Your filter looks like it is running early. Try a later priority. – Nathan Powell Oct 31 '16 at 18:37
  • To clarify what @NathanPowell said, you need to change your add_filter line to add_filter('body_class', 'prefix_remove_body_class', 99, 1);. Anything above 10 should work, I think, so 99 is probably an overkill. – Dmitriy Gamolin Oct 31 '16 at 19:26
  • Thank you for the suggestion. However, increasing the priority did not work. I still suspect that what I'm trying to do may not be possible. i.e. the body_class hook runs before the template loads, leading the function to run too late to effect the body class. – psorensen Nov 1 '16 at 16:45

Your Answer

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

Browse other questions tagged or ask your own question.