Php get template directory url

get_template_directory_uri() │ WP 1.5.0

get_bloginfo(‘template_url’); — это алиас этой функции — при запросе этой опции будет вызвана эта фукнция.

  • Используйте get_stylesheet_directory_uri(), когда используется дочерняя тема и нужно получить её URL.
  • Используйте get_template_directory(), когда нужно получить путь до папки темы.
  • Используйте plugin_dir_url(), когда нужно получить URL для плагина.
Хуки из функции

Возвращает

Использование

get_template_directory_uri();

Примеры

#1 Использование функции в HTML теге

Для безопасности результат функции нужно очищать через esc_url() или esc_attr().

Впрочем такую очистку нужно делать абсолютно для всех функций которые используются в атрибутах тегов. Например, если такой очистки не будет, то ваш код не пройдет проверку при размещении темы/плагина в офф репозитории. Пример очистки:

#2 Получим путь до шаблона:

echo get_template_directory_uri(); // получим: http://example.com/wp-content/themes/theme_name

#3 Используем функцию, чтобы подключить скрипт:

add_action( 'wp_enqueue_scripts', 'my_scripts_method' ); function my_scripts_method()< wp_enqueue_script( 'custom_script', get_template_directory_uri() . '/js/custom_script.js', ['jquery'] ); >

Список изменений

Код get_template_directory_uri() get template directory uri WP 6.2.2

function get_template_directory_uri() < $template = str_replace( '%2F', '/', rawurlencode( get_template() ) ); $theme_root_uri = get_theme_root_uri( $template ); $template_dir_uri = "$theme_root_uri/$template"; /** * Filters the active theme directory URI. * * @since 1.5.0 * * @param string $template_dir_uri The URI of the active theme directory. * @param string $template Directory name of the active theme. * @param string $theme_root_uri The themes root URI. */ return apply_filters( 'template_directory_uri', $template_dir_uri, $template, $theme_root_uri ); >

Cвязанные функции

theme path url (папка темы)

Подключение файлов темы

Доброе время суток! Подскажите, какой подход в плане скорости работы лучше? Сохранять эту функцию в переменную

$templatePath = esc_url( get_template_directory_uri() )
style comment-meta / comm_meta_js"> 
0
9 месяцев назад #
Php get template directory url
Kama9392

Логичнее:

$templatePath = esc_url( get_template_directory_uri() );

Источник

get_template_directory_uri() │ WP 1.5.0

Retrieve current theme directory URI. Returns the URL to the root theme, not a child one. Doesn't contain a closing slash at the end. You can also use get_bloginfo('template_url'); instead of this function.

Hooks from the function

Return

Usage

get_template_directory_uri();

Examples

#1 Get the URL to the theme

echo get_template_directory_uri(); // output: http://example.com/wp-content/themes/theme_name

#2 Including a script

add_action( 'wp_enqueue_scripts', 'my_scripts_method' ); function my_scripts_method() < wp_enqueue_script( 'custom_script', get_template_directory_uri() . '/js/custom_script.js', ['jquery'] ); >

#3 Using a function in an HTML tag

To be safe, the result of the function should be cleared with esc_url() or esc_attr(). This cleanup should be done for all functions which are used in tag attributes. For example, if this cleanup is not done, your code will not pass the check when placing the theme/plugin in the official repository. An example of cleaning:

Changelog

get_template_directory_uri() get template directory uri code WP 6.2.2

function get_template_directory_uri() < $template = str_replace( '%2F', '/', rawurlencode( get_template() ) ); $theme_root_uri = get_theme_root_uri( $template ); $template_dir_uri = "$theme_root_uri/$template"; /** * Filters the active theme directory URI. * * @since 1.5.0 * * @param string $template_dir_uri The URI of the active theme directory. * @param string $template Directory name of the active theme. * @param string $theme_root_uri The themes root URI. */ return apply_filters( 'template_directory_uri', $template_dir_uri, $template, $theme_root_uri ); >

theme path (theme folder url)

Theme files connection

Источник

get_template_directory_uri()

Если нужно получить URL дочерней темы, когда она используется, то используйте функцию get_stylesheet_directory_uri().

Когда нужно получить путь до папки темы, используйте get_template_directory()

Когда нужно получить такой URL для плагина, используйте plugin_dir_url()

Примеры

#1. Получим путь до шаблона:

echo get_template_directory_uri(); // получим: http://example.com/wp-content/themes/theme_name

#2. Используем get_template_directory_uri(), чтобы подключить скрипт:

add_action( 'wp_enqueue_scripts', 'my_scripts_method' ); function my_scripts_method() < wp_enqueue_script( 'custom_script', get_template_directory_uri() . '/js/custom_script.js', ['jquery'] ); >

Код get template directory uri: wp-includes/theme.php WP 5.2.4

function get_template_directory_uri() < $template = str_replace( '%2F', '/', rawurlencode( get_template() ) ); $theme_root_uri = get_theme_root_uri( $template ); $template_dir_uri = "$theme_root_uri/$template"; /** * Filters the current theme directory URI. * * @since 1.5.0 * * @param string $template_dir_uri The URI of the current theme directory. * @param string $template Directory name of the current theme. * @param string $theme_root_uri The themes root URI. */ return apply_filters( 'template_directory_uri', $template_dir_uri, $template, $theme_root_uri ); >

Источник

Get Theme Directory In WordPress

When you doing theme development there are times when you need to reference a file in the same folder of the current theme. You may need to include a file in your code or add a stylesheet to your website. There are many different ways you can include a file in your code, WordPress comes with a number of constants that you can use in your development to get paths to your wp-content folder or plugin folders.

WP_CONTENT_DIR // no trailing slash, full paths only WP_CONTENT_URL // full url WP_PLUGIN_DIR // full path, no trailing slash WP_PLUGIN_URL // full url, no trailing slash

These are hardcoded constants so if you want to reference a specific theme you will need to hardcode the theme name in your code.

$twentyThirteenTheme = WP_CONTENT_DIR . '/themes/twentythirteen/'; $twentyThirteenThemeStylesheet = WP_CONTENT_URL . '/themes/twentythirteen/style.css';

The problem you have with this is that if your theme name changes then it will break your code. WordPress comes with a number of useful functions you can use to get the current theme path or URL.

 get_template_directory_uri() get_stylesheet_directory_uri() get_stylesheet_uri() get_theme_root_uri() get_theme_root() get_theme_roots() get_stylesheet_directory() get_template_directory()

get_template_directory_uri()

This function will return the URL of the current theme, it will not return a trailing slash. If you are using a child theme then this function will return the parent theme directory URL.

Use this function to include a new Stylesheet or Javascript file in your theme.

  add_action('wp_enqueue_scripts', 'my_scripts_method'); ?>

get_stylesheet_directory_uri()

This function will return the URL for the current theme, if you are running a child theme then this will return the child theme directory URL.

This can be used exactly the same way as the get_template_directory_uri().

  add_action('wp_enqueue_scripts', 'my_scripts_method'); ?>

get_stylesheet_uri()

The above functions will return the URL for the directory of the current theme, this function will return the URL of the current theme stylesheet URL. If you are using the child theme then this will return the child theme stylesheet URL.

get_theme_root_uri()

THis function will return the URL to the theme directory.

get_theme_root()

This function will return the file path of the themes directory without a trailing slash.

get_theme_roots()

Return an array of themes located in the themes directory.

get_stylesheet_directory()

This function will return the the full file path to the current theme directory, like the get_stylesheet_directory_uri() this will return the current theme even if it's being used as the child theme.

Use this function to include files in your application from the current theme.

get_template_directory()

This function will return the full file path to the current theme directory, like the get_template_directory_uri() if you are using a child theme then this function will return the path to the parent theme directory.

Use this function to include files in your application from the current theme.

Источник

Читайте также:  Телеграм бот оплата python
Оцените статью