Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Include a Default Placeholder Image In Absence of Image Value WordPress

  • SOLVED

Hi Guys,

Working with the ClassiPress classifieds theme I want to have a default image showing up if someone has not set an url to their logo image.

This is the line of code in the author.php template that shows the logo image if a logo url has been submitted via the profile page:

<div id="user-logo"><img src="<?php echo $curauth->logo_id; ?>" alt="" /></div>

On the profile page I have this code added for the submission of the logo image url:

<tr>
<th><label for="twitter"><?php _e('Logo:','appthemes')?></label></th>

<td>
<input type="text" name="logo_id" id="logo_id" value="<?php echo esc_attr(get_the_author_meta('logo_id', $user->ID)); ?>" class="regular-text" size="35" /><br />
<span class="description"><?php _e('Enter your logo image URL - Include the http:// part in front of the url','appthemes')?></span>
</td>
</tr>


And the logo_id value has been added on the tpl_profile.php template page:

update_usermeta($user_ID, 'logo_id', $_POST['logo_id']);
update_usermeta($user_ID, 'twitter_id', $_POST['twitter_id']);
update_usermeta($user_ID, 'facebook_id', $_POST['facebook_id']);
update_usermeta($user_ID, 'paypal_email', $_POST['paypal_email']);


So far so good, this works.


Now comes the issue at hand,

If no logo image is submitted there is an ugly broken image symbol showing on other browsers then FireFox.

So a placeholder image is required, so if in the absence of a logo image url a placeholder image will be used, and if there is a logo image url the placeholder image is swapped out for the logo image.


I've come up with this function snippet to be placed in appthemes-functions.php

// give us either the uploaded logo pic or a placeholder
function appthemes_get_logo_pic() {
echo '<div id="user-logo"><img src="<?php echo $curauth->logo_id; ?>" alt="" /></div>';
} else {
echo '<div id="user-logo"><img src="'.get_bloginfo("template_url").'/images/no-logo.jpg" /></div>';

}
}


And instead of this line:

<div id="user-logo"><img src="<?php echo $curauth->logo_id; ?>" alt="" /></div>

I would use this:

<?php appthemes_get_logo_pic();?>

But it produces a parse error, so I'm not coding this right syntax wise.

Secondly, I'm not sure if this would even be a viable solution?

So a solution to set up a default image in the absence of a logo image url would be much appreciated. And of course if a logo image is present the default image needs to be swapped out for the submitted logo image.





Answers (2)

2011-03-06

Hai Bui answers:

Hi.
Please modify your function like this:

function appthemes_get_logo_pic() {
if ($curauth->logo_id)
echo '<div id="user-logo"><img src="'.$curauth->logo_id.'" alt="" /></div>';
else echo '<div id="user-logo"><img src="'.get_bloginfo("template_url").'/images/no-logo.jpg" /></div>';
}


Hai Bui comments:

If it doesn't work, put the code directly on the template:

instead of
<div id="user-logo"><img src="<?php echo $curauth->logo_id; ?>" alt="" /></div>
use
<?php
if ($curauth->logo_id)
echo '<div id="user-logo"><img src="'.$curauth->logo_id.'" alt="" /></div>';
else echo '<div id="user-logo"><img src="'.get_bloginfo("template_url").'/images/no-logo.jpg" /></div>';
?>

2011-03-06

S├ębastien | French WordpressDesigner answers:

On the profile page, replace

<input type="text" name="logo_id" id="logo_id" value="<?php echo esc_attr(get_the_author_meta('logo_id', $user->ID)); ?>" class="regular-text" size="35" /><br />

by

$slh = get_the_author_meta('logo_id', $user->ID) ? esc_attr(get_the_author_meta('logo_id', $user->ID)) : get_bloginfo("template_url").'/images/no-logo.jpg;
<input type="text" name="logo_id" id="logo_id" value="<?php echo $slh; ?>" class="regular-text" size="35" /><br />