Title: Problem with template_include and body_class
Last modified: May 27, 2020

---

# Problem with template_include and body_class

 *  [ideacom](https://wordpress.org/support/users/ideacom/)
 * (@ideacom)
 * [6 years ago](https://wordpress.org/support/topic/problem-with-template_include-and-body_class/)
 * I’m using the filter of template_include :
 *     ```
       add_filter( 'template_include', 'custom_template');
       function custom_template( $template ) {
       if ( is_page( 66 )  ) {
       $template = get_query_template( 'template-test' );	
       }
       return $template;
       }
       ```
   
 * Everything work fine outside the fact I dont get the class “page-template-template-
   test.php” in the body class. I instead get “page-template-default”
 * From what I understand, template_include is called after the body tag so body_class
   dosent get to work with the new template. Is it normal?
 * Am I using the function properly?
 * Thank
    -  This topic was modified 6 years ago by [ideacom](https://wordpress.org/support/users/ideacom/).

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [Joy](https://wordpress.org/support/users/joyously/)
 * (@joyously)
 * [6 years ago](https://wordpress.org/support/topic/problem-with-template_include-and-body_class/#post-12901305)
 * As it says in the Code Reference [https://developer.wordpress.org/reference/hooks/template_include/](https://developer.wordpress.org/reference/hooks/template_include/)
 * > This filter hook is executed immediately before WordPress includes the predetermined
   > template file.
 * which means that it has nothing to do with the body_class function. If your template
   doesn’t call `body_class()`, then that would cause a problem.
    But it sounds 
   as if you do since you say you get the page-template-default class. This makes
   sense to me since that represents the page template that the user chose in the
   editor, not the name of the template file being used.
 * As for using the filter properly, what are you trying to achieve, and where is
   your code?
    If your code is in a theme, remove it since themes shouldn’t be looking
   at specifics of post IDs. If your code is in a child theme, it would be better
   if you use the [Template Hierarchy](https://developer.wordpress.org/themes/basics/template-hierarchy/),
   and simply name the template page-66.php and no filter is needed. If your code
   is in a plugin, the check for page should be more generic, and the path should
   include the path to the plugin (since it will look in the theme folder).
 *  Thread Starter [ideacom](https://wordpress.org/support/users/ideacom/)
 * (@ideacom)
 * [6 years ago](https://wordpress.org/support/topic/problem-with-template_include-and-body_class/#post-12901461)
 * Thank [@joyously](https://wordpress.org/support/users/joyously/) for the quick
   answer.
 * As for your question :
 * My code goes into a plugin. That code is just a simplify version to work with
   in function.php for testing purpose.
 * As for what i’m trying to achieve : i’m trying to replicate woocommerce way of
   building template with [https://gist.github.com/bmarshall511/8e00dd4acd081c3bd1f27bae22122e34](https://gist.github.com/bmarshall511/8e00dd4acd081c3bd1f27bae22122e34)
   as guide. I create template in my plugin folder, associate them to certains page.
   Then the user can create his own child template if he desire in the theme folder
   to overwrite the template.
 * > This makes sense to me since that represents the page template that the user
   > chose in the editor, not the name of the template file being used.
 * At this point maybe this is the direction the wordpress team went with. I don’t
   find it logical since in my case the user didn’t select any template at all. 
   In any case this is fine with me, I will try to find another way to handle the
   template.
 * Thank.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Problem with template_include and body_class’ is closed to new replies.

## Tags

 * [body_class](https://wordpress.org/support/topic-tag/body_class/)
 * [template_include](https://wordpress.org/support/topic-tag/template_include/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 2 replies
 * 2 participants
 * Last reply from: [ideacom](https://wordpress.org/support/users/ideacom/)
 * Last activity: [6 years ago](https://wordpress.org/support/topic/problem-with-template_include-and-body_class/#post-12901461)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
