Improve child theme file inheritance by introducing `theme_url()` to locate a file URI
|Reported by:||johnbillion||Owned by:||westi|
|Component:||Themes||Keywords:||has-patch westi-likes needs-unit-tests|
Child theme file inheritance isn't quite as slick as it could be. A child theme should be able to selectively override any of its parent theme's files (ie. CSS & JS), not just template files.
wp_enqueue_script( 'foo', get_template_directory_uri() . '/foo.js' );
A child theme couldn't simply include a foo.js file to override its parent's as the file is always loaded from the template directory, not the stylesheet directory. If the parent theme used get_stylesheet_directory_uri() instead, then the child theme could override it but it would have to override it, otherwise we'd end up with a file not found.
The answer is to introduce a function that does the same as locate_template() but returns a URI instead of a path.
wp_enqueue_script( 'foo', locate_theme_file( 'foo.js' ) );
This would load foo.js from the child theme if it existed, and the parent theme if not.
The function could also be used for CSS files:
wp_enqueue_style( 'bar', locate_theme_file( 'bar.css' ) );
And for images too:
<img src="<?php echo locate_theme_file( 'icon.png' ); ?>" />
Stand by for a patch.
Change History (95)
- Keywords westi-likes added
- Owner set to westi
- Status changed from new to reviewing
- Keywords dev-feedback removed
- Milestone changed from Awaiting Review to 3.3
- Summary changed from Improve child theme file inheritance by introducing a function to locate a file URI to Improve child theme file inheritance by introducing `theme_url()` to locate a file URI