Enqueuing Files
In our site we can enqueue ( load in front end ) script or styles as per our need.
Instead of directly ( custom ) loading those files by hardcoding script or link tags in our theme/template, we can take advantage of the functions WordPress provides us,
- wp_register_style()
- wp_register_script()
- wp_enqueue_script()
- wp_enqueue_style()
WordPress first registers all the scripts and styles that means it stores the information related to the same inside variables/objects
For e.g.
Information/data related to scripts are stored under $wp_scripts global object.
Information/data related to styles are stored under the $wp_styles global object.
We also provide a unique handle name to our scripts/style while registering them, which we can later use to enqueue the same script in our code.
For example.
wp_register_style( 'style-1', get_stylesheet_directory() . '/style-1.css' );
wp_register_style( 'style-2', get_stylesheet_directory() . '/style-2.css' );
switch ( $post_id ) { // just for a test, can be any condition
case 1:
wp_enqueue_style( 'style-1' );
break;
case 2:
wp_enqueue_style( 'style-2' );
break;
case 3:
// no style to enqueue
break;
}Some benefits about the register first and enqueue later are:
- We can register our script/style once and trigger the enqueue command later multiple times on the basis of condition, so instead of repeating our src or other code again and again we can just call the enqueue for the handle. -> Explained in above example.
- In case, our plugin/theme does not use the style/script we have registered, it can be used by other plugin/themes if they want to include it.
Template Parts
In our templates we can divide it into several parts for the code reusability to prevent us from copy pasting and repeating the code everywhere,
for example if we have a code that displays the content of our page, it can be used at various places, so instead of repeating the code we can add the code inside parts.
Generally, template parts are defined as follows, for the example of content
/template-parts/content.php/template-parts/content-none.php/template-parts/content-excerpt.php
Now if we want to include it into our main template file, instead of include or require, WordPress provides us with a function get_template_part() which we can take advantage for this functionality
get_template_part( $slug, $name = null, $args = array() )
Inside this function, how it works is, that it creates an array for the name of the possible file-names
for example if we run the below code
get_template_part( 'template-parts/content', 'excerpt' );Inside the function, it creates an array with elements as
- template-parts/content-excerpt.php
- template-parts/content.php
Later on it would provide this array to the function locate_template to get the file required in the script.
Locate template function would iterate through the array and require the first file that exists.
Code for the locate template function :
locate_template( $template_names, $load = false, $require_once = true, $args = array() )Template Tags
These are tags which we use in our template files to display the information dynamically, they usually echo the information directly to the page.
Some general template tags are as follows:
- get_header()
- get_footer()
- get_sidebar()
- get_template_part()
- get_search_form()
- wp_logout_url()
- wp_login_url()
- wp_login_form()
- wp_lostpassword_url()
- bloginfo()
