Why blogInfo() Doesn't Work in Inline CSS and How to Fix It
You cannot place a PHP function like <?php bloginfo('template_directory'); ?> directly inside a standard CSS rule block within a WordPress theme. The snippet you provided attempts to execute server-side code inside a stylesheet, which the browser interprets as literal text rather than executable logic. When a user visits your site, they receive the raw HTML string containing the PHP tags, not the resolved file path. Consequently, the background-image property fails to load because the browser looks for a file named literally <?php bloginfo('template_directory'); ?>/images/parallax_image.jpg.
In standard web development, there is a fundamental separation between server-side logic and client-side rendering. CSS files are sent to the browser as static text; they do not execute PHP scripts. To get a background image working in WordPress, you must resolve the file path on the server before sending the final HTML to the client.
Using Theme Functions in the Output Buffer
The most common and robust solution is to apply your CSS to an element that exists in the page's HTML output, rather than trying to embed PHP directly into a <style> block. You should target a specific container or body element using CSS selectors defined in your theme's stylesheet (e.g., style.css) or a dedicated .css file loaded via the enqueue function.
In that stylesheet, you can use WordPress template functions that are available when the page is rendered. However, standard style.css files are processed by WordPress during the build process for some themes, or they are served as static assets if using a child theme structure where styles are enqueued. If you need to output dynamic content within the <head> tag specifically for CSS variables or inline styles, you must use PHP tags outside of the CSS block.
For example, if you are working with a specific section like a header or hero area, you might have an HTML element with an ID like #hero-section. You can set the background image using a PHP variable that holds the path:
<?php
$template_dir = get_template_directory();
$background_path = get_template_directory_uri() . '/images/parallax_image.jpg';
?>
<style>
#hero-section {
background-image: url(<?php echo esc_url($background_path); ?>);
background-size: cover;
background-position: center;
}
</style>
This approach works because the PHP code executes on the server, generating a valid URL string (e.g., https://yoursite.com/wp-content/themes/my-theme/images/parallax_image.jpg) which is then embedded into the HTML sent to the browser. The <style> block becomes static HTML containing that resolved path.
Using Custom Post Types or Page Attributes
If you want different background images on different pages without editing every file, a better architectural pattern involves using WordPress meta data or page attributes. You can store the image path in the post content or use a specific meta box plugin to define the background for that page. Then, your CSS can check for these values and apply them dynamically.
Alternatively, modern build tools and theme frameworks often allow you to pass variables from PHP to CSS using preprocessors like Sass or Less. If you are using a framework like Oxygen Builder or a custom theme with a preprocessor pipeline, you can define paths in the functions.php file and expose them as SCSS variables, allowing your stylesheet to reference them cleanly without embedding raw PHP tags inside the final output if the build step handles the replacement.
Performance and Caching Considerations
When setting background images via CSS, performance is a critical factor. If you hardcode paths using get_template_directory_uri(), ensure the path includes the full URL scheme (http:// or https://). Relative paths can sometimes break if the stylesheet is loaded from a CDN or if WordPress's content delivery network (CDN) settings alter the root directory structure.
Furthermore, consider the impact on caching. Static CSS files with hardcoded background images are excellent for caching because the browser can store them long-term. However, if you use PHP variables to dynamically inject paths, ensure that the resulting HTML output is not causing cache invalidation issues in your CDN or server-level caching setup (like Redis or Varnish). If the path changes frequently, you might need to bust the cache by appending a version query string to the image URL.
Conclusion on Implementation Strategy
The core issue with the original snippet was attempting to execute PHP inside a static CSS context without proper execution environment. By moving the logic into the template file where HTML is generated, or by using theme functions that resolve paths correctly before the style is applied, you achieve the desired visual result. Always ensure your image paths are absolute URLs when used in inline styles within dynamic HTML blocks to prevent 404 errors caused by relative path mismatches.
