Make WordPress Core


Ignore:
Timestamp:
09/08/2016 10:53:57 PM (10 years ago)
Author:
johnbillion
Message:

Themes: Improve child theme file inheritance by introducing functions for locating and fetching the URL or path to files within child and parent themes.

The most useful function this introduces is get_theme_file_uri(), which returns the URL to the specified file in the child theme if it exists, and falls back to the URL to the specified file in the parent theme. This allows parent themes to reference files (including enqueuing CSS and JavaScript files) that can be overridden by the child theme simply by existing.

This change also introduces get_theme_file_path(), which is the file path equivalent of get_theme_file_uri().

Finally, get_parent_theme_file_uri() and get_parent_theme_file_path() are also introduced, which allow a theme to specifically reference a file URL or file path in the parent theme. These can be used as replacements for get_template_directory_uri() and get_template_directory() respectively, for consistency.

Props johnbillion, georgestephanis, gma992.
Fixes #18302

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/link-template.php

    r38470 r38578  
    931931
    932932        $tax = get_taxonomy( $term->taxonomy );
    933         if ( ! $tax || ! current_user_can( $tax->cap->edit_terms ) ) {
     933        if ( ! $tax || ! current_user_can( 'edit_term', $term->term_id ) ) {
    934934                return;
    935935        }
     
    985985
    986986        $tax = get_taxonomy( $term->taxonomy );
    987         if ( ! current_user_can( $tax->cap->edit_terms ) )
     987        if ( ! current_user_can( 'edit_term', $term->term_id ) ) {
    988988                return;
     989        }
    989990
    990991        if ( empty( $link ) )
     
    40244025        return apply_filters( 'get_avatar_data', $args, $id_or_email );
    40254026}
     4027
     4028/**
     4029 * Retrieve the URL of a file in the theme.
     4030 *
     4031 * Searches in the stylesheet directory before the template directory so themes
     4032 * which inherit from a parent theme can just override one file.
     4033 *
     4034 * @since 4.7.0
     4035 *
     4036 * @param string $file Optional. File to search for in the stylesheet directory.
     4037 * @return string The URL of the file.
     4038 */
     4039function get_theme_file_uri( $file = '' ) {
     4040        $file = ltrim( $file, '/' );
     4041
     4042        if ( empty( $file ) ) {
     4043                $url = get_stylesheet_directory_uri();
     4044        } elseif ( file_exists( get_stylesheet_directory() . '/' . $file ) ) {
     4045                $url = get_stylesheet_directory_uri() . '/' . $file;
     4046        } else {
     4047                $url = get_template_directory_uri() . '/' . $file;
     4048        }
     4049
     4050        /**
     4051         * Filter the URL to a file in the theme.
     4052         *
     4053         * @since 4.7.0
     4054         *
     4055         * @param string $url  The file URL.
     4056         * @param string $file The requested file to search for.
     4057         */
     4058        return apply_filters( 'theme_file_uri', $url, $file );
     4059}
     4060
     4061/**
     4062 * Retrieve the URL of a file in the parent theme.
     4063 *
     4064 * @since 4.7.0
     4065 *
     4066 * @param string $file Optional. File to return the URL for in the template directory.
     4067 * @return string The URL of the file.
     4068 */
     4069function get_parent_theme_file_uri( $file = '' ) {
     4070        $file = ltrim( $file, '/' );
     4071
     4072        if ( empty( $file ) ) {
     4073                $url = get_template_directory_uri();
     4074        } else {
     4075                $url = get_template_directory_uri() . '/' . $file;
     4076        }
     4077
     4078        /**
     4079         * Filter the URL to a file in the parent theme.
     4080         *
     4081         * @since 4.7.0
     4082         *
     4083         * @param string $url  The file URL.
     4084         * @param string $file The requested file to search for.
     4085         */
     4086        return apply_filters( 'parent_theme_file_uri', $url, $file );
     4087}
     4088
     4089/**
     4090 * Retrieve the path of a file in the theme.
     4091 *
     4092 * Searches in the stylesheet directory before the template directory so themes
     4093 * which inherit from a parent theme can just override one file.
     4094 *
     4095 * @since 4.7.0
     4096 *
     4097 * @param string $file Optional. File to search for in the stylesheet directory.
     4098 * @return string The path of the file.
     4099 */
     4100function get_theme_file_path( $file = '' ) {
     4101        $file = ltrim( $file, '/' );
     4102
     4103        if ( empty( $file ) ) {
     4104                $path = get_stylesheet_directory();
     4105        } elseif ( file_exists( get_stylesheet_directory() . '/' . $file ) ) {
     4106                $path = get_stylesheet_directory() . '/' . $file;
     4107        } else {
     4108                $path = get_template_directory() . '/' . $file;
     4109        }
     4110
     4111        /**
     4112         * Filter the path to a file in the theme.
     4113         *
     4114         * @since 4.7.0
     4115         *
     4116         * @param string $path The file path.
     4117         * @param string $file The requested file to search for.
     4118         */
     4119        return apply_filters( 'theme_file_path', $path, $file );
     4120}
     4121
     4122/**
     4123 * Retrieve the path of a file in the parent theme.
     4124 *
     4125 * @since 4.7.0
     4126 *
     4127 * @param string $file Optional. File to return the path for in the template directory.
     4128 * @return string The path of the file.
     4129 */
     4130function get_parent_theme_file_path( $file = '' ) {
     4131        $file = ltrim( $file, '/' );
     4132
     4133        if ( empty( $file ) ) {
     4134                $path = get_template_directory();
     4135        } else {
     4136                $path = get_template_directory() . '/' . $file;
     4137        }
     4138
     4139        /**
     4140         * Filter the path to a file in the parent theme.
     4141         *
     4142         * @since 4.7.0
     4143         *
     4144         * @param string $path The file path.
     4145         * @param string $file The requested file to search for.
     4146         */
     4147        return apply_filters( 'parent_theme_file_path', $path, $file );
     4148}
Note: See TracChangeset for help on using the changeset viewer.