views:

75

answers:

3

Hi guys, I am trying to create a sidebar for which I can specify the image in the back-end of my wordpress cms using custom fields, now I have gotten it to work, with just one little bug, if the user enters a invalid URL, the image link will display as broken and will not display, is there a way that I can hide the broken image icon perhaps?

I have a background image set for the parent DIV element so that if there is no image to display, the parent's background will.

here is the PHP code:

//here I get the 'side_image' custom field, which will contain the URL to the side image    
if (have_posts()) : 
         while (have_posts()) : the_post(); 
             $side = get_post_meta($post->ID, 'side_image', true); 
         endwhile;
 endif;

HTML:

<!--here is the HTML markup-->
<div id="inner_content_right">
    <img src="<?php echo $side; ?>" />
</div>

CSS:

#inner_content_right {
    background: url(images/Layout_3_other_06_backup.jpg) no-repeat;
    width: 259px;
    height: 691px;
    float: right;
    position: relative;
    bottom: 28px;
}

Thanx in advance!

+1  A: 

You can test it with cURL. See the answer at this link.

fabrik
Thanx for the post, it helped me along the right path! ;)
You're welcome :)
fabrik
+1  A: 

You could try something like

<!--here is the HTML markup-->
<div id="inner_content_right">
    <img src="<?php if (@getimagesize($side)) echo $side; ?>" />
</div>
Oliver
I'm not really into suppressing error messages :o
fabrik
@fabrik me neither. But in this case, the whole question is about suppressing an error situation, so that a fault-tolerant check-routine seems right. If it breaks, the picture just doesn't get displayed instead of some-kind of gd error-message. At least for the visitor's of the blog a more pleasant experience :)
Oliver
Thanx guys, I got it to work! I will post the working code!
A: 

Thanx guys, I got it to work with this code!

//check if the string is a valid URL
function checkURL($url)
{
    return preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $url);
}

//returns a image with a valid URL or nothing at all
function validateImage($one){
if(!checkURL($one))
{
    $errMsg .= "Please enter valid URL including http://";
    //return $errMsg;
} else {
    $headers = get_headers($one, 1);
    $return = $headers[0];
    if($return!='HTTP/1.1 404 Not Found'){
        $string = "<img src='$one' />";
        return $string;
    }
    }
}

Thanx for all your help!