Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between get_stylesheet_directory_uri() and get_template_directory_uri() when enqueuing scripts in wordpress

Tags:

wordpress

It's not a serious question.

I normally use get_stylesheet_directory_uri() when enqueuing scripts in WordPress, which worked fine so far.

I just wonder what's the difference between get_stylesheet_directory_uri() and get_template_directory_uri() when enqueuing scripts in wordpress.

According to WordPress Codex:

  • get_template_directory_uri -> Retrieve template directory URI for the current theme.
  • get_stylesheet_directory_uri ->Retrieve stylesheet directory URI for the current theme/child theme

Then, get_template_directory_uri cannot be used for a child theme?

like image 353
Peter Avatar asked Aug 03 '15 21:08

Peter


People also ask

What is Get_stylesheet_directory_uri?

get_stylesheet_directory_uri() Retrieves stylesheet directory URI for the active theme.

How do I find the template directory in Wordpress?

Use get_stylesheet_directory() to get the absolute path to the child theme directory. To retrieve the URI of the stylesheet directory use get_stylesheet_directory_uri() instead.


1 Answers

Both functions can be used in a parent or a child theme.

get_template_directory_uri will always refer to the parent theme folder for assets.

get_stylesheet_directory_uri will refer to the "current" theme folder for assets (which could be the parent or the child, depending on where it is called).

For example, in a child theme:

// This will point to style.css in child theme wp_enqueue_style( 'my_child_styles', get_stylesheet_directory_uri().'/style.css' );  // This will point to style.css in the parent theme wp_enqueue_style( 'my_parent_styles', get_template_directory_uri().'/style.css' ); 

Note that if a theme is not a child theme, then it is considered a parent theme.

like image 170
Ben Cole Avatar answered Sep 23 '22 13:09

Ben Cole