Wordpress plug-in to identify unlinked files
The most important URL and file path functions for WordPress theme developers at a glance
As a beginner in WordPress theme development, the various functions for returning the theme folder URL or the file path can be a bit confusing at the beginning. In this post I would therefore like to give an overview of the various URL and path functions in WordPress and their use.
Difference between url and path
WordPress has both functions for returning the absolute path to the theme folder, as well as functions for the output of the theme folder Url. Both types are important for theme developers.
The absolute path can be thought of as the local storage location of the theme files on the web server (e.g. / home / user / public_html / wp-content / themes /). This is required, for example, to load PHP files in the theme and execute them internally on the server.
WordPress URL and path functions at a glance
WordPress has four standard functions for returning the theme URL or the theme path, which have been in the core since time immemorial (version 1.5).
get_stylesheet_directory and get_stylesheet_directory_uri
The two get_stylesheet_directory Functions give the path / url of the currently activated Themes of the WordPress installation. If a child theme is used, these functions refer to the folder of the Child themes.
absolute path to the child theme folder
e.g. / home / user / public_html / wp-content / themes /child-theme
URL to the child theme folder
get_template_directory and get_template_directory_uri
The two get_template_directory Functions, on the other hand, always relate to that Parent theme.
absolute path to the parent theme folder
e.g. / home / user / public_html / wp-content / themes /parent-theme
URL to the parent theme folder
Note: The WordPress documentation sometimes also mentions stylesheet directory and template directory. The former refers to the child theme folder due to its functions, the latter the parent theme folder.
Common problem: Using a child theme
Without a child theme, all four functions refer to the same folder - that of the current theme. There are therefore no errors, regardless of whether get_template_directory or get_stylesheet_directory or their URL functions are used.
Often it only becomes problematic when a child theme is activated.
Then the correct functions must be used to load the files. Using the wrong function may result in the wrong theme folder in which the files do not exist.
Application of the functions in the WordPress theme
When which function is used depends on the situation. In the following I would like to show the typical use cases.
Loading PHP files
The functions of a WordPress theme are located in the functions.php. Most themes are a bit more extensive and divide functions into several files for better clarity.
Therefore, in many themes, the functions.php more files loaded.
For loading PHP files we need the absolute path, not the URL.
We therefore receive the correct integration of the files from the (parent) theme folder get_template_directory (). This means that the files can still be included when users create a child theme. The function continues to search for the file in the parent theme.
If you created a child theme and created a new PHP file there that should be loaded, you have to get_stylesheet_directory () use to return the path of the child theme folder.
Incidentally, it does not matter whether the functions are used in the child or parent theme. The difference between Child & Parent relates to the return value of the functions, not in which context they are executed.
Excursus: For loading Template files WordPress provides extra functions like get_template_part (), which I would like to present in a separate post soon. Therefore only function files should be integrated in the way described here.
Again, we use it again get_template_directory_uri, because the corresponding JS and CSS files are in the parent theme. Often a child theme just consists of style.css, functions.php and a few template files.
But if there are new CSS and JS files in Child theme can be added instead, the function get_stylesheet_directory_uri be used.
For the sake of completeness, I also mention the loading of static images, which I personally rarely need in my themes. Also again with URL function.
Special function for the style.css
WordPress knows a special function for integrating the style.css: get_stylesheet_uri ()
This actually only connects get_stylesheet_directory () with the string /style.css. Nevertheless, the special function should always be used because it provides the filter and thus gives plugins access to the loading of the style.css.
New features in WordPress 4.7
With WordPress 4.7, four new functions were recently introduced, which complement and partially replace the old functions. I will present this in a separate article next week.
- What irritates you in public traffic
- What song do your ears like
- Why alanine is called alanine
- Why are security tokens important
- Do certain cultures or societies cause violence?
- Is capitalism inherently unfair
- Why do cats have no opinions
- Which Beretta 9mm is the best
- Hillary Clinton has immunity from prosecution
- What is the mitochondria of bacteria
- How can we find a multi-excavator share
- What are the limits of copyright protection
- Are stocks with a higher beta better
- How to correct ambiguous sentences
- Are two children much heavier than one
- How important is morning coffee to you
- What is something that grinds your gears
- When was shampoo first used in China
- Which companies would buy Birchbox
- What is the best Frank Sinatra biography
- Why do people choose Thumsup over Pepsi
- Keeping ETG tests in court
- Which company will Apple Inc. succeed?
- Are men attracted to noisy women 1
- Payday loans are legal
- Is a broken bone a disease 1
- Is Notepad ++ better than other HTML editors
- How does a chicken casserole taste
- What's the point of Shriners
- Computer science is difficult in 11th grade
- What is the favorite color of the BTS members
- What are some new Vocaloid songs
- How does a boy get talent
- What are neurosciences