How can I get the image URL in a WordPress theme?

How can I get the image URL in a WordPress theme?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

How can I get the image URL in a WordPress theme?

When you are developing a custom WordPress theme, it is common to store assets like icons or decorative graphics inside the images/ directory within your theme root. The code snippet you provided places an <img> tag directly into index.php, pointing straight to images/mindset.jpg. While this works in a static HTML context, it often fails in WordPress because of how the server handles file paths relative to the webroot versus the theme root.

The core issue is that when a browser requests a page served by Apache or Nginx, URLs are resolved relative to the site's document root (usually public_html or www). If your images folder sits inside /wp-content/themes/your-theme/images/, a direct reference to images/mindset.jpg tells the browser to look for that file at the domain root. Since WordPress themes reside deeper in the directory structure, the server returns a 404 error because it cannot find the file at the expected location.

To resolve this, you must generate the correct URL based on the theme's active installation path. WordPress provides specific functions designed to handle asset loading safely and consistently across different hosting environments.

Using get_stylesheet_directory_uri()

The most robust method for retrieving paths to local assets is using get_stylesheet_directory_uri(). This function returns the full URL of the currently active child theme or parent stylesheet directory. It automatically calculates the path from the webroot to your theme folder, regardless of whether you are running on a local development server or production hosting.

Update your code in index.php as follows:

<img src="<?php echo get_stylesheet_directory_uri() . '/images/mindset.jpg'; ?>" 
     width="145" height="32" alt="Mindset Icon">

By concatenating the output of get_stylesheet_directory_uri() with your relative path, you ensure the browser requests the file from /wp-content/themes/your-theme/images/mindset.jpg, which is the correct location. This approach is preferred over hardcoding paths because it works even if a child theme overrides the parent theme's files; the URL will dynamically adjust to point to the active theme's assets.

Handling Parent Themes and Child Themes

If you are developing a child theme and rely on images located in the parent theme's images/ folder, using get_stylesheet_directory_uri() will return the URL for your child theme directory. If your child theme does not have an images/ folder, this function returns the URL of the child theme root (often empty or pointing to a non-existent location), and the concatenated path will be incorrect.

In scenarios where you need to access assets from the parent theme specifically, you should use get_template_directory_uri() instead:

<img src="<?php echo get_template_directory_uri() . '/images/mindset.jpg'; ?>" 
     width="145" height="32" alt="Mindset Icon">

This function returns the URL of the parent theme directory. If your development setup involves copying assets from the parent to the child, ensure you are referencing the correct source. However, best practice dictates that asset paths should be relative to the theme being loaded by WordPress, not hardcoded to a specific folder structure inside wp-content.

Why Hardcoded Paths Fail

Avoid using PHP variables like $theme_dir or assuming the site is served from /var/www/html. These approaches break immediately if you switch hosts, move the WordPress installation to a subdirectory (e.g., example.com/blog/), or deploy via a containerized environment. Functions like get_stylesheet_directory_uri() abstract away these underlying server configurations, adhering to WordPress's standard loading mechanism.

If you require further assistance with theme architecture or asset management strategies that align with modern development workflows, resources like LaravelCompany often provide insights into scalable PHP application structures, though their primary focus is the Laravel framework rather than WordPress core functions. For pure WordPress theming, sticking to the built-in directory URI functions remains the industry standard for reliability.

Final Considerations

Always include alt attributes when serving images loaded via PHP concatenation. While this is a semantic requirement, it also aids in debugging; if the image fails to load, screen readers and search engines understand what the asset represents. Additionally, consider using the <img> tag with the loading="lazy" attribute if these images are not above the fold, to improve initial page load performance.

Our network