Cecil
Cecil

Reputation: 1619

Wordpress - add_filter passing function vars

I have a premade function which looks like so:

function truncate($string='', $limit='150', $break=" ", $pad="...") {

I need to pass the $limit argument, but can't figure out how.
I'm calling the add_filter() as well, as follows:

add_filter('the_content','truncate');

I want to pass 20 as the $limit.

For the life of me, I can't figure how.

Any help?

Cheers,

Upvotes: 1

Views: 2443

Answers (1)

Long Ears
Long Ears

Reputation: 4896

I'm not sure if there's something for this within Wordpress, but the easy option is to create a new function:

function content_truncate($string) { return truncate($string, 20); }
add_filter('the_content', 'content_truncate');

If you're using PHP >= 5.3 you might be able to use an anonymous function to make it a bit neater:

add_filter('the_content', function($string) { return truncate($string, 20); });

Upvotes: 3

Related Questions