There is another idea significant of specifying before we bounce into our crude code. Activities and channels are two totally diverse ideas which relate profoundly in the ways they control module information.
These two bits of code come standard inside of the WordPress API. Channels and activities consider module engineers to redesign bits of code all through the WordPress administrator board relating to your new module. This implies you could add another tab in the sidebar or extra settings joins for your Plug-in alternatives.
A filter is used on a bit of text or data being passed into WordPress. With filters you are quite literally able to filter content through your own custom written functions to change data in any way.
For example, you may create a filter to change $the_content which is a variable set by WordPress containing the entire post content of a WordPress article. For our plug-in we will be taking $the_content and shortening the length of characters into an excerpt.
Filters come in handy when you are writing plug-ins to customize the looks and feel of your blog. These are especially popular when writing sidebar widgets or smaller functions to change how a post should be displayed. Below is a sample line of code showing how to apply a filter.
Here we are adding a filter into the WordPress page title. Note this code doesn’t relate to our official plugin and is only being used as an example here.
The add_filter function is native to WordPress and used to add a new filter to a variable found within page content. In the line above we’re targeting $wp_title which contains the title of our current page. We are then passing this variable into a fake function titled web_func() which could then manipulate and return a new title tag for whatever purposes.
Actions are similar to filters in that they don’t work on bits of data but instead target pre-defined areas in your templates and admin panel. Below is a small list of example actions for you to get familiar with some of the pre-defined target areas.
- publish_post – called when a post is published or when status is changed into “published”
- save_post – called when a post/page is created from start or updated
- wp_head – called when the template is loaded and runs the wp_head() function
- loop_end – called immediately after the final post has been processed through the WordPress loop
- trackback_post – called whenever a new trackback is added into a post
Again we can see how simple this bit of code boils down to. If you can understand the difference between actions and filters you’ll be that much closer to building comprehensive, working WordPress plugins. Below is another line of code initializing an action function on the save_post hook. To clarify again this doesn’t pertain to our current developing plugin and is only used as a piece of example code to understand the add_action() function.