Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want the smilies to be generated in my sidebar (where it shows the recent posts, comments etc.) and not being displayed as text only.

I've been searching through the Wordpress Codex, Google and here, but couldn't find any answers.

Is this possible at all anyways?

How would I do it for recent comments that I display in the sidebar?

<?php   $comments = get_comments('status=approve&number=5'); ?>
<?php foreach ($comments as $comment) { ?>
<li><p><strong><?php
        $title = get_the_title($comment->comment_post_ID);
        echo get_avatar( $comment, '45' );
echo strip_tags($comment->comment_author); ?></strong>&nbsp;commented on <a href="<?php echo get_permalink($comment->comment_post_ID); ?>#comment-<?php echo $comment->comment_ID; ?>" rel="external nofollow" title="<?php echo $title; ?>"> <?php echo $title; ?></a>: <?php echo wp_html_excerpt( $comment->comment_content, 45 ); ?> (...)</p></li>
<?php }  ?>

Thanks in advance.

share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

WordPress converts text emoticons to their image equivilents with a function appropriately named convert_smilies.

To get smilies to show up, say, in text widgets, you would need to run the widget text through that convert_smilies function. You do this by adding a filter:

<?php
add_filter( 'widget_text', 'convert_smilies' );

Of maybe you wanted to add smilies to widget titles:

<?php
add_filter( 'widget_title', 'convert_smilies' );

If you want to display smilies in recent posts, it gets a bit more tricky. The recent posts widget, for instance, uses functions like get_the_title and such to retrieve elements of the posts.

So you could add smilies to post titles:

<?php
add_filter( 'the_title', 'convert_smilies' );

But that's not going to allow you to conditionally make sure that smilies only show up in post titles on the sidebar.

If you're using third-party plugins for widgets, you're going to have to poke around their code yourself and see if there are any filters into which you can hook.

EDIT:

In your case, you probably just need to change this bit of code:

<?php echo wp_html_excerpt( $comment->comment_content, 45 ); ?>

to

<?php echo wp_html_excerpt( convert_smilies( $comment->comment_content ), 45 ); ?>
share|improve this answer
    
Thank you very much! I updated my question. How would I go about doing that for recent comments (see code in my question). Thanks again :) –  japanworm Oct 2 '11 at 1:38
    
See my edit. Should get you pointed in the right direction. –  chrisguitarguy Oct 2 '11 at 4:58
    
Hm. Thanks, but this doesn't work. It just makes the excerpt stop before a smilie (e.g. Comment content: "Test :smilie:" becomes now "Test" and the excerpt just stops after that even if there's more text to be displayed after the smilie ... –  japanworm Oct 2 '11 at 5:59
1  
So, you need to think a bit about what the functions you're using do. Start small: first convert_smilies replaces textual emoticons with images. That's not going to cause our block of text to get cut of. So go to the next higher level: wp_html_excerpt. If you take a look inside that function, the first thing is does is strip all html tags. So obviously our images are not going to make it. So switch the order: <?php echo convert_smilies( wp_html_excerpt( $comment->comment_content, 45 ) ); ?> That's why I said the above would, "get you started." –  chrisguitarguy Oct 2 '11 at 13:41
    
Awesome, thank you! I'm not really good with coding at all. This is my first website ever, so I'm really thankful for the help! :) –  japanworm Oct 2 '11 at 15:14
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.