user1427661
user1427661

Reputation: 11774

PHP Doing Strange Things with URLs in Embedded HTML Tags

I generate some URLs at the beginning of each iteration of a for loop on an PHP/HTML page, like this:

if ($chart_track->positionchange == 0) {
    $position_change_image = bloginfo('template_directory') . "/images/equal.png";
}
else if ($chart_track->positionchange > 0) {
    $position_change_image = bloginfo('template_directory') . "/images/up.png";
}
else {
    $position_change_image = bloginfo('template_directory') . "/images/down.png";
}

I then echo the $position_change_image into the src attribute of an img tag, like this:

<img src="<?php echo $position_change_image; ?>" class="position-image">

This creates a broken link AND outputs the base of the URL (the template_directory part of it) in plain text. Upon inspecting the img src, it only has the final part i.e., /images/up.png. I try wrapping $position_change_image in an esc_url function, but that doesn't work either. When I echo the actual value of $position_change_image elsewhere, it's the full URL. Why in the name of God is it getting broken up and output like this?

Upvotes: 0

Views: 55

Answers (1)

Zevi Sternlicht
Zevi Sternlicht

Reputation: 5399

Thats because you are calling echo twice because bloginfo echos automatically!

Instead use get_bloginfo.

Bloginfo Docs:

Displays information about your site, mostly gathered from the information you supply in your User Profile and General Settings WordPress Administration Screens. It can be used anywhere within a template file. This always prints a result to the browser. If you need the values for use in PHP, use get_bloginfo().

Upvotes: 2

Related Questions