I’m doing some custom theme work for the first time and I want to use
<?php get_template_directory(); ?>/main.css
to access the CSS file. However, when this renders in the html, it renders as:
/main.css
It’s possible I deleted whatever setting or option that WordPress uses to derive this path. What logic does WordPress use to determine what the template directory is?
As a test, I added this code:
DIRS:
Plugins
<?php plugins_url(); ?>
<?php plugin_dir_url(); ?>
<?php plugin_dir_path(); ?>
<?php plugin_basename(); ?>
Themes
<?php get_template_directory_uri(); ?>
<?php get_stylesheet_uri(); ?>
<?php get_theme_root_uri(); ?>
<?php get_theme_root(); ?>
<?php get_theme_roots(); ?>
Site Home
<?php home_url(); ?>
WordPress
<?php admin_url(); ?>
<?php site_url(); ?>
<?php content_url(); ?>
<?php includes_url(); ?>
<?php wp_upload_dir(); ?>
Multisite
<?php get_admin_url(); ?>
<?php get_home_url(); ?>
<?php get_site_url(); ?>
<?php network_admin_url(); ?>
<?php network_site_url(); ?>
<?php network_home_url(); ?>
and it rendered as:
DIRS:
Plugins
Themes
Site Home
WordPress
Multisite
So it’s pretty clear that I messed up something WordPress needs to determine it’s directories. But I can’t find anything in Google about how WordPress comes up with these paths and the source code isn’t obvious to me either.
get_template_directory
returns a string. It doesn’t echo that string. This is very common behavior for WordPress functions that start withget_
. Try:Depending on what you plan to do, you can also use: