Adding Custom Stylesheets to wp_head in TwentyThirteen
You are currently injecting your stylesheets outside of the WordPress core hook, which breaks the expected lifecycle of asset loading. In the standard TwentyThirteen theme (and most modern themes), the <head> section is split into two distinct blocks: static meta tags defined by the theme developer, and the dynamic wp_head() action hook managed by WordPress.
The code snippet you provided places your custom CSS links before <?php wp_head(); ?>. This is problematic because it bypasses the queueing mechanism that handles dependency management and prevents other plugins or themes from correctly calculating their own output. To add files properly, you must enqueue them using the WordPress API within a function hooked to wp_enqueue_scripts.
The Enqueue Function Pattern
The standard approach involves creating a function in your child theme's functions.php file that calls wp_enqueue_style(). This function should be hooked into the wp_head action. However, it is more efficient to hook it to wp_enqueue_scripts, which runs earlier in the load order specifically for registering assets.
Here is how you structure the code:
function twenty_thirteen_add_custom_css() {
// Enqueue your custom stylesheet
wp_enqueue_style( 'my-custom-style', get_stylesheet_directory_uri() . '/css/my-custom.css', array(), '1.0.0' );
}
add_action( 'wp_enqueue_scripts', 'twenty_thirteen_add_custom_css' );
By using get_stylesheet_directory_uri(), you ensure the path resolves correctly relative to your child theme directory. The second argument in wp_enqueue_style() is an array of dependencies. If your custom CSS relies on Bootstrap (as seen in your snippet), you might want to declare that dependency:
wp_enqueue_style( 'my-custom-style', get_stylesheet_directory_uri() . '/css/my-custom.css', array( 'bootstrap' ), '1.0.0' );
Note that for this to work, the handle 'bootstrap' must be registered first, either by the theme itself or via your own wp_enqueue_style call for the Bootstrap CSS file found in your snippet.
Why This Approach is Necessary
Placing <link> tags directly in the HTML head, as you are doing now, bypasses WordPress's asset management system. When you enqueue styles properly, WordPress tracks them internally. This allows plugins that might need to modify your stylesheet (e.g., adding print-specific rules or admin-only styles) to do so by hooking into add_filter( 'style_loader_src' ) or similar mechanisms.
Furthermore, using wp_head() directly in a child theme is generally discouraged for enqueueing assets because it runs after other plugins have already added their own scripts and styles. If you enqueue your custom CSS inside the wp_head hook itself, you risk loading it after critical scripts that depend on those styles are executed, potentially causing layout shifts or broken UI states.
Handling Bootstrap Dependencies
In your specific example, you are loading a local copy of Bootstrap (bootstrap.css) and its JavaScript counterpart. If you want to integrate this cleanly into wp_head, you must ensure the Bootstrap stylesheet is registered before your custom CSS tries to extend it.
If your custom CSS modifies the Bootstrap grid or components, you should enqueue the standard Bootstrap library (via CDN or a local handle) first, then enqueue your custom file as a dependency on that handle:
function twenty_thirteen_enqueue_assets() {
// Register and enqueue Bootstrap if not already done by the parent theme logic
wp_register_style( 'bootstrap', get_stylesheet_directory_uri() . '/bootstrap.css' );
wp_enqueue_style( 'bootstrap' );
// Now enqueue your custom styles dependent on Bootstrap
wp_enqueue_style( 'my-custom-style', get_stylesheet_directory_uri() . '/css/my-custom.css', array( 'bootstrap' ), null );
}
add_action( 'wp_enqueue_scripts', 'twenty_thirteen_enqueue_assets' );
This ensures that when my-custom.css is parsed by the browser, the DOM already contains the Bootstrap definitions it relies on.
Conclusion on Implementation
Moving your manual <link> tags into the PHP enqueue system restores the integrity of the rendering pipeline. It allows for version control via the third argument in wp_enqueue_style() and enables future optimization techniques like cache busting or conditional loading based on screen size. By adhering to this pattern, you maintain compatibility with WordPress core updates and third-party plugins without introducing fragile manual HTML manipulation into your theme's header.