Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Filters are hooks that we can use to edit content in WordPress. This can be done either as content is output to the browser--or some other content reader--or is saved to the database. You can use filters to add to, remove from, or otherwise modify textual data. Your task as a WordPress plugin developer is to find the right filter and then make the right change to the text. We are going to use the add filter method to add this, but the first thing we need to do is create a function. You can see I have created another plugin here in the plugins directory: feed_copyrighter.
I have set all the information in the comments, so I am ready to go. I am now going to write a filter function. Now with filters, the function itself is a little different than with actions. The function itself is going to take an argument, which will be the content. So again, I am going to use this prefix cwmp. This just keeps a unique name on my functions, so that there's no collision with other functions in WordPress, and we will call this add_content_watermark. Now here is the difference: content.
So notice I am taking in data. So this is going to be the raw content, whatever is I am filtering on. It's going to depend on the filter. I might be filtering a comment. I might be filtering a post or a page or something like that. That's going to be the content. So in here what I am going to do is I am first going to test to see if the content is a feed, so that's a built-in WordPress function. So if it is a feed then I am going to return the content just as it is. I am not actually going to change it, except for I am going to append to it, "Created by Falkon Productions," and let's put a little space in here first, and then we'll say, copyright.
Now we can use the date function to output the year, and then we will add on, "all rights reserved." So what that will do then, if this is a feed, it will append it on. So think about your web site. Your web site probably has a footer with copyright information, but if someone accesses your RSS feed, it may not have that. So this will add it to everything in the RSS feed. And then lastly, I am going to have sort of a default return, in case it's not a feed, essentially, that will just return the raw content.
Now to add this as a filter, I use the add_filter method. It's going to take the name of the filter-- in this case, the_content-- this filter loops through each of the posts that gets output, so it'll get output in each post. And then I put my function name. It's not a bad idea to just copy it and paste it in. That way I can make sure I at least avoid making a typo, especially when you have this prefix that's a rather long function name. So now I can go into my WordPress admin console.
Here's my Content Watermark Plugin. I'll activate it. The RSS feed can be found at any site by typing in the URL of the site and then afterwards appending this variable feed equals, and in this case, rss2. There are actually some other formats you can use, but rss2 will be fine. So you can see here is our plugin feed before we added that plugin. If I refresh it, you can see it adds in my copyright. So that's the watermark. If I want to remove a filter, I follow basically the same format--only I use remove_filter and I apply that to the filter and pass whatever function.
Of course, I don't want to do that, so I am not going to do that. There are lists of all these different functions. The Plugin API that we looked at earlier has a filter reference. This can be really helpful when you're first getting started. So WordPress filters, as their name implies, allow developers to filter, to edit the actual content of WordPress. This can be done either before the information is saved to the database or before it's sent out to the client. To write filters, write a function that does the filtering. It's going to take the argument, which is going to be the content, and it's going to return the filtered content after having done whatever you want it to do in the function.
Then you add the hook by using the add _filter function, and finally if you should want to remove a filter, call the remove_filter function.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 104631 Viewers
56 Video lessons · 116553 Viewers
71 Video lessons · 85732 Viewers
131 Video lessons · 41026 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.