how to remove all body classes in wordpress

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

Topic body-class page-template Wordpress

Category Web


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 );

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 );

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.