add_filter shortcuts in WordPress

Once you start developing more with WordPress you’ll probably find yourself using add_filter to add, modify or remove data.

When modifying some text you might do something like the following simple example.

https://gist.github.com/nickdavis/6403c81b4ec05ba32afecc97634a589e

Sometimes you’ll want to remove text too.

https://gist.github.com/nickdavis/fd8a1c75c6f68389f6f087a9f63357df

However, you might not be aware (or might forget!) that there’s a shorter way of doing this.

https://gist.github.com/nickdavis/71c2f1f06287c6f4347b7a8e8ef67c6b

In the above example, there’s no need to write out a separate function to achieve the result that you want. Instead you can use one of the following (self explanatory) ‘shortcut’ functions, baked into WordPress, to pass the type of data that you want.

https://gist.github.com/nickdavis/c4afa54d97ab6a69e1d6eecdf3268fd7

Writing it out this way is not only shorter, but much easier to read too.

Leave a Reply

Your email address will not be published.