Displaying images in assets / images folder from functions.php file

I need to reference a image from functions file, and I was figuring out how to do it.
I think you can use get_theme_file_uri() and that will give you the URL to the theme folder.

I’ve tested with get_theme_file_uri() but it returns http://sage.test/wp-content/themes/sage-demo while get_template_directory_uri() returns http://sage.test/wp-content/themes/sage-demo/resources .

The problem is when I do a yarn build:productionthe image change the name and It doesn’t work.

I’ve tried with @asset but it doesn’t work on functions file. This is what I actually have:

add_filter( 'wp_postratings_site_logo', 'wp_postratings_site_logo' );
function wp_postratings_site_logo( $url ) {
    return get_theme_file_uri() . '/dist/images/logo.svg';
}

Any help here?

Thanks in advance

Hey @demssite,

Try the asset_path() function from helpers.php (which is what the @asset Blade directive uses behind the scenes.

Not tested, but should be something like this:

add_filter( 'wp_postratings_site_logo', 'wp_postratings_site_logo' );
function wp_postratings_site_logo( $url ) {
    return asset_path('/images/logo.svg');
}

If you’re not in the App namespace, prefix that when you make the call: App\asset_path('my/file.ext')

2 Likes

Working like a charm @mmirus but I don’t understand well how namespaces work. Do you know where can I find more info about this?

Thanks in advance.
Diego

Hey @demssite,

It can be a little confusing initially. Here are a couple of resources:

Hope that helps!