Ashley Brown Ashley Brown - 5 months ago 10
HTML Question

Using a WP filter makes a value disappear?

I'm using this to display some tweets.

After using the override filters as shown here, it makes the timestamp that naturally occurs to disappear.

The author gives us an example that you can use to override the default markup like so:

add_filter('latest_tweets_render_tweet', function( $html, $date, $link, array $tweet ){
$pic = $tweet['user']['profile_image_url_https'];
return '<p class="my-tweet"><img src="'.$pic.'"/>'.$html.'</p><p class="my-date"><a href="'.$link.'">'.$date.'</a></p>';
}, 10, 4 );


Here is my version:

add_filter('latest_tweets_render_tweet', function($html){
return
'<div class="row">
<div class="small-1 columns twitter-icon-wrap">
<i class="fa fa-twitter tweet-icon fa-2x fa-pull-left"></i>
</div>
<div class="small-11 columns tweet-wrap">'.$html.'</div>
<p class="tweet-details"><a href="" target="_blank"></a></p>
</div>';
}, 10 , 1 );


If I add
$date
after the
$html
variable in the function like:

add_filter('latest_tweets_render_tweet', function($html, $date)


I then get a warning:

Warning: Missing argument 2 for ******\******\Extras\{closure}(), called in /srv/www/*********/current/web/wp/wp-includes/plugin.php on line 235 and defined in /srv/www/*******/current/web/app/themes/*********/lib/extras.php on line 144


line 144 is
add_filter('latest_tweets_render_tweet', function($html)


If I subsequently add the
$date
variable into the returned HTML ignorning the warning, I then get an error:

Notice: Undefined variable: date in.....


Although it's a warning, there is still no date displayed. How can I make the date re-appear?

Answer

The issue is the final argument in add_filter() (1). This parameter is the number of accepted arguments.

add_filter ( string $tag, callable $function_to_add, int $priority = 10, int $accepted_args = 1 )

Since you want to use 2 arguments, you need to change the final argument of add_filter() to 2. In other words:

add_filter ( 'latest_tweets_render_tweet', function( $html, $date ) {}, 10, 2 );

It's unclear what exactly you're trying to do with $date...so I'll just give you a generic example:

add_filter('latest_tweets_render_tweet', function( $html, $date ){
    // $html and $date are now available to you
    return true;  // remember to return something (likely something different than this)
}, 10, 2 );  // <-- This argument is changed to 2
Comments