0

i am working on custom page templates. so i want to remove all body classes by one function only. i searched it for on net but not found any solution.

is there anyone who can answer it. @Php

Edit..

how to remove all page template classes except all other..

example - page-template, page-template-zx, page-template-zx-php etc. means all classes that starts with "page" word.

problem is that i am using many page templates so i want to do this with one function from pasting in functions.php

5
  • Which classes in particular do you mean?
    – Rup
    Commented Jun 21, 2021 at 16:33
  • classes of body tag in wordpress, dude.
    – user208055
    Commented Jun 21, 2021 at 16:36
  • 1
    @E-Gyan why do you want to do this? page templates themselves are a html class on the body tag, you can use that to write CSS rules that eliminate any potential conflicts. Note that any answer you get won't be a copy pasta, you will need a basic understanding of programming to use the answers you get here
    – Tom J Nowell
    Commented Jun 21, 2021 at 16:50
  • @TomJNowell and i have enought programming knowledge to use them sir
    – user208055
    Commented Jun 21, 2021 at 16:52
  • @TomJNowell if you can then please answer it.\
    – user208055
    Commented Jun 21, 2021 at 16:52

2 Answers 2

5

I'm going to assume you mean the classes generated by body_class(), e.g. from the twentytwentyone header.php:

<body <?php body_class(); ?>>
<?php wp_body_open(); ?>

The simplest thing to do is to just remove the <?php body_class(); ?> call from your header.php. Or if you can't / don't want to change that, create a new header.php for these pages e.g. header-custom.php and load this with wp_head('custom') in your template.

Or if you really do need to suppress the output of body_class() then you can filter that away:

function empty_body_class($classes, $class) {
    return [];
}
add_filter( 'body_class', 'empty_body_class', 999, 2 );

but you'll probably be left with an empty class="" on the body tag.


Or (as you've asked in comments) if you just want to remove anything starting "page", or a fixed string "example_class", you can just edit the array in the filter instead e.g.

function filter_body_classes( $classes, $class ) {
    foreach( $classes as $key => $value ) {
        if ( strpos( $value, 'page' ) === 0
             || $value === 'example_class' ) {
            unset( $classes[$key] );
        }
    }
    return $classes;
}
add_filter( 'body_class', 'filter_body_classes', 999, 2 );
5
  • how to remove all page template classes except all other
    – user208055
    Commented Jun 21, 2021 at 16:40
  • example - page-template, page-template-zx, page-template-zx-php etc. means all classes that starts with "page" word
    – user208055
    Commented Jun 21, 2021 at 16:45
  • problem is that i am using many page templates so i want to do this with one function from pasting in functions.php
    – user208055
    Commented Jun 21, 2021 at 16:46
  • Just change the string I'm testing for in the strpos line then
    – Rup
    Commented Jun 21, 2021 at 17:01
  • now what changes i have to made your last function if i want to remove another class like "example_class"
    – user208055
    Commented Jun 21, 2021 at 17:02
0

The solution from Rup should answer your question. As you've asked in comments "what changes I have to made your last function if I want to remove another class like 'example_class'", you can just edit the line I have mentioned in my following code:

function filter_body_classes( $classes, $class ) {
    foreach( $classes as $key => $value ) {
        if ( strpos( $value, 'page' ) === 0
             || $value === 'example_class' || $value === 'example_class_two' || $value === 'example_class_three' ) { //add as many example_class as you need like this
            unset( $classes[$key] );
        }
    }
    return $classes;
}
add_filter( 'body_class', 'filter_body_classes', 999, 2 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.