Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

In my theme I use locate_template() to load the various parts of my functions library. I'm looking for a way to not include one of those files when using a child theme. I tried adding locate_template('same_file_name.php', false); to my child theme's functions.php but it is still included.

CLARIFICATION EDIT: I don't want to prevent this file from being used by child themes. In general I want this file to be included when I am using a child theme. I am looking for something to add to a child theme when I don't want to use it.

share|improve this question
    
Does same_file_name.php exist in you child theme? –  s_ha_dum Sep 12 '13 at 0:03
    
In general (ie in the starter child theme I always start from) yes, but in the cases were I don't need it I can delete it. –  JPollock Sep 12 '13 at 21:03
1  
Upvoted the answer by @G.M because I think you are going to need something like that. –  s_ha_dum Sep 12 '13 at 21:22

3 Answers 3

up vote 2 down vote accepted

An alternative solution that takes inspiration from both answers by @Milo and @toscho.

create your own function to load template, e.g.

if ( ! function_exists('my_locate_template') ) {
  function my_locate_template( $template = '', $load = false, $once = true ) {
    $filtered = apply_filters('allow_child_load_' . $template, true);
    if ( ! is_child_theme() || $filtered ) return locate_template($template, $load, $once);
    return false;
  }
}

Then in your parent theme load files using my_locate_template('same_file_name.php').

In this way your files will be always loaded in parent theme and when using child theme, you can use the filter to exclude some files.

add_filter('allow_child_load_disallow-this.php', '__return_false'); 

and after that the file disallow-this.php, will not be loaded in that child theme.

Also note the function is wrapped in if (! function_exists('my_locate_template') ) { so, if you want, you can completely replace it in a child theme.

Simple, flexible, and with comfortable defaults.

share|improve this answer
    
This would remove the file whenever a child theme is used. I need to be able to choose to remove it from specific child themes. Updating question to make this more clear. –  JPollock Sep 12 '13 at 21:02
    
@JPollock by default it removes the file in every child themes, but in child theme functions.php via the allow_child_load_{$file} filter, you can choose to allow the loading of specific files. So in every child themes you have control upon every file loading or not. –  G. M. Sep 12 '13 at 21:06
    
Yes, but I'd like the file to be included by default. In specific cases I want to remove it. –  JPollock Sep 12 '13 at 21:08
1  
@JPollock now the function is opt-out: by defaults it include files, and via filter you can exclude some files. –  G. M. Sep 12 '13 at 21:15
    
Brilliant stuff. This works perfectly for files in the theme, but what about if the file I was trying to disallow was in a subdirectory? –  JPollock Sep 13 '13 at 5:56

You could offer a filter for these files in your parent theme. Then you can decide in each child theme separately which files should be loaded:

$files_to_include = array(
    'foo.php',
    'bar.php'
);
$files_to_include = apply_filters(
    get_template() . '_files_to_include',
    $files_to_include
);

if ( ! empty ( $files_to_include ) )
{
    foreach ( $files_to_include as $file )
        locate_template( $file );
}

In a child theme you can remove some files from that array now:

add_filter( get_template() . '_files_to_include', function( $files )
{
    $bar_key = array_search( 'bar.php', $files );

    if ( FALSE !== $bar_key )
        unset( $files[ $bar_key ] );

    return $files;
});
share|improve this answer

You could wrap the locate_template call in a check for is_child_theme in your parent theme so it will be ignored:

if( ! is_child_theme() ){
    locate_template( ... );
}
share|improve this answer
    
This would remove the file whenever a child theme is used. I need to be able to choose to remove it from specific child themes. Updating question to make this more clear. –  JPollock Sep 12 '13 at 21:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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