Ashley Brown Ashley Brown - 5 months ago 12
HTML Question

How to correctly use this filter in wordpress?

I'm working with this Twitter plugin. I output it's content by using the

do_shortcode
function within my template.

Within their other notes section, it says I can override the custom HTML markup that it will output the tweets within a list object by using
add_filter('latest_tweets_render_list')


However, when I do

add_filter('latest_tweets_render_list', function( array $list ){
return '<div>'.$list.'</div>';
}, 10, 1);


I get a PHP warning stating that there is an
array to string conversion
and it outputs no tweets.

I took a look in the plugins core files and there is this.

function latest_tweets_render_html( $screen_name = '', $num = 5, $rts = true, $ats = true, $pop = 0 ){
$items = latest_tweets_render( $screen_name, $num, $rts, $ats, $pop );
$list = apply_filters('latest_tweets_render_list', $items, $screen_name );
if( is_array($list) ){
$list = '<ul><li>'.implode('</li><li>',$items).'</li></ul>';
}
return
'<div class="latest-tweets">'.
apply_filters( 'latest_tweets_render_before', '' ).
$list.
apply_filters( 'latest_tweets_render_after', '' ).
'</div>';
}


How do I correctly use the
latest_tweet_render_list
filter in order to override the default HTML markup? I have tried to use a foreach loop but that didn't do the trick either. I do not wish to modify the plugin files if it's avoidable.

Answer

Your callback method is passed an array - you can't simply output an array (hence your message about the conversion). Depending on what is in the array, you can do something like this:

add_filter('latest_tweets_render_list', function( array $list ){
  $output = '<div>';
  foreach ($list as $l) {
    $output .= $l;
  }
  $output .= '</div>';
  return $output;
}, 10, 1);

To see the items in your $list variable, you can use var_dump($list) at the top of your function first.