Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress WordPress Hooks - Actions and Filters Filter Functions in WordPress The add_filter Function

Dayne Wright
Dayne Wright
11,235 Points

Clarification on add_filter and Zac's custom function

I think I understand how the add_filter works and the need to be able to search the code base to find the filter you need to add the additional filter to. I am just confused by the $length argument that he is adding into the custom function. Is it just a place holder? How does it know what value to pass into the function? (I think this may be a PHP question)

I think part of my problem is getting a grasp on what variables are just placeholders and what variables are being passed.
Hopefully I am making some sense. :)

<?php
// add_filter example hooking into the excerpt_length filter
function my_custom_excerpt_length( $length ) {
    return 20;

}
add_filter( 'excerpt_length', 'my_custom_excerpt_length', 999 );

?>

2 Answers

Kevin Korte
Kevin Korte
28,148 Points

So in this case, Zac went back and looked where the the apply_filters call was being made for the 'excerpt_length'. It shows it at 3:19 in the video. We know that this filter than is giving us the length of the excerpt, and we can see that it is 55 in this case. So we know our custom filter will be passed the excerpt length by wordpress. Zac uses in this case a $length variable. But because we don't care what the length is, we just want to change it to 20, we ignore what WP gave us and return what we want.

In a different case, we could do something like return $length * 2 so that whatever the excerpt length was from wordpress, our filter doubled it. That is a cheesy example of how you might dynamically change some aspect of WP based off of what the current or default value is.

Does that help? This is a tricky part.

Dayne Wright
Dayne Wright
11,235 Points

I understand that it gives us 55 as the length and we can see it from the excerpt_length. I am still confused on the $length he is passing in the function. Could that be any variable or is it coming from somewhere? So, could the same function work with function my_custom_function ( $random ) { return $random * 2; }

I am getting lost on what things you are defining elsewhere and you need to locate them vs. being just placeholders within a function. I guess the bottom line is I am not grasping the scope as it relates to filters and actions.

Kevin Korte
Kevin Korte
28,148 Points

Oh gottcha, to answer your question yes, you could change $length to $random and it would still work. This part of your question does belong to PHP functions in general. The $length variable Zac created could be anything, and it's scope is entirely limited to the scope of the function. $length would not be available outside of the function.

When you're writing a function that is going to accept arguments, you can accept those argument values into whatever valid variable name you want to use in the function. Even if that variable exists outside of the function, you can use it again because of scope. The only thing that matters is the order of the arguments if your function accepts multiple.

Kevin Korte
Kevin Korte
28,148 Points

You're welcome! Glad I could help.

Mohamed Hak
Mohamed Hak
17,195 Points

Thank you Kevin Korte, I have the same problem and you made it clear.

What about if we have no interest in doing anything to the parameter being passed in can we just do function my_custom_excerpt_length() {return 20;} without passing the parameter in? Or are we always required to pass in the parameters to the callback according to the required parameters of the filter we are trying to hook? Hope I'm explaining myself