Oromis
Oromis

Reputation: 11

get_stylesheet_directory_uri() and get_template_directory_uri() not returning "http"

I've made a local copy using xampp of my wordpress website. The theme was written by me. Locally, every image link using get_template_directory_uri() or get_stylesheet_directory_uri() is broken because it is missing the "http" prefix. If I add it manually, then the image is displayed correctly.

This is the code: <?php echo get_stylesheet_directory_uri() . '/images/myimage.jpg' ?>

the above functions online generate:
http://www.mywebsite.it/wp-content/themes/mytheme/images/myimage.jpg"
locally on xampp they generate:
localhost/myfolder/wp-content/themes/mytheme/images/myimage.jpg
and the image is not displayed. If I manually add http:// before localhost it works.

Interestingly enough, the function get_template_directory_uri() is also used in functions.php to enqueue some stylesheets, and there it generates the correct url beginning with http even if locally.

I don't know hot to fix this, I don't want to change every link here locally, and having to remember to fix it back before putting it online. Sorry for my english.

Edit: I was able to automatically add "http" by escaping the url, with this code
<?php echo esc_url(get_stylesheet_directory_uri() . '/images/myimage.jpg') ?>
This fixes the problem locally, and doesn't affect the output online, therefore I can sync the code like this... still, I don't know why it behaved like this.

Upvotes: 0

Views: 1356

Answers (1)

Ananthakrishnan Baji
Ananthakrishnan Baji

Reputation: 1290

Please check WordPress Address (URL) and Site Address (URL) in Settings

Upvotes: 1

Related Questions