As you can see, the default excerpt length is still 150, but we’ve also applied
some filters to it. A filter allows you to write a function that modifies the
value of something — in this case, the excerpt’s length. The name (or tag) of
this filter is excerpt_length, and if no functions are attached to it, then its
value will remain 150. Let’s see how we can now use this to modify the
default value.
First, we have defined a function that does nothing but return a number. At
this point, nothing is using the function, so let’s tell WordPress that we want
to hook this into the excerpt_length filter.
We’ve successfully changed the default excerpt length in WordPress,
without touching the original function and without even having to write a
custom excerpt function. This will be extremely useful, because if you
always want excerpts that are 200 characters long, just add this as a filter
and then you won’t have to specify it every time.
Suppose you want to tack on some more text, like “Read on,” to the end of
the excerpt. We could modify our original function to work with a hook and
then tie a function to that hook, like so:
This hook is placed at the end of the function and allows us to modify its
end result. This time, we’ve also passed the output that the function would
normally produce as a parameter to our hook. The function that we tie to
this hook will receive this parameter.
All we are doing in our function is taking the original contents of $excerpt
and appending our “Read on” text to the end. But if we choose, we could
also return the text “Click the title to read this article,” which would replace
the whole excerpt.