I Love Code
I Love Code

Reputation: 420

Wordpress image path is correct but image does not display on site

I have a very simple piece of PHP code where I get the image upload path in Wordpress, and if I insert the path into my browser the image is found is displays correctly, so I know the path is correct. But on the site it does not display. I am working on localhost.

Here is what I have tried:

$uploads = wp_upload_dir();

$path = $uploads['basedir'] . '/12hours/';

date_default_timezone_set('Africa/Johannesburg');

$hour = date("H");

if ($hour == 10) {
    echo 'it is 10am <br>';
    echo '<img src="' . $path . '6-60.jpg" data-src="' . $path . '6-60.jpg" class="img-fluid lazy" alt="something" />';
}

When I inspect the element on my site the path looks like this:

/Users/myname/Sites/mysite/wp-content/uploads/12hours/6-60.jpg

And when I insert the path in a new tab in the browser the image does load.

What could the issue be?

Upvotes: 2

Views: 446

Answers (1)

ksiger
ksiger

Reputation: 323

basedir is a server path, but baseurl is URL to upload directory.
Try this:

$uploads = wp_upload_dir();
$path = $uploads['baseurl'] . '/12hours/';
date_default_timezone_set('Africa/Johannesburg');
$hour = date("H");
if ($hour == 10) {
    echo 'it is 10am <br>';
    echo '<img src="' . $path . '6-60.jpg" data-src="' . $path . '6-60.jpg" class="img-fluid lazy" alt="something" />';
}

Upvotes: 2

Related Questions