Difference and usage of uri (e.g. get_directory_uri) and absolute path (e.g. get_directory)

What is the difference uri (e.g. get_directory_uri) and absolute path (e.g. get_directory) and when it the best time to use each?

Topic directory urls permalinks links Wordpress

Category Web


I'm assuming you're referring to:

get_template_directory() is an absolute path - so if you're including a PHP file that is in your template directory this would be a good function to use. If you're using a child theme and need to reference something in the parent theme you could also use this function.

get_template_directory_uri() is the URL to the theme directory: http://www.yourdomain.com/wp-content/themes/yourtheme/ - if you're using a child theme and need to link to something in the parent theme you could use this. If you're using a single theme and want to enqueue a Javascript file you could use this function to easily get your theme folder:

function my_scripts_method() {
    wp_enqueue_script(
        'custom_script',
        get_template_directory_uri() . '/js/custom_script.js',
        array('jquery')
    );
}
add_action('wp_enqueue_scripts', 'my_scripts_method');

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.