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.

add_filter():-
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.

add_filter(‘wp_title’, ‘web_func’);

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.
add_action():-
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.

add_action(‘save_post’, ‘notify’);


0 thoughts on “WP Filters and Actions”

Leave a Reply

Related Article

Blog

How to add user profile image by custom URL and custom fields in woocommerce

If you want to add user profile image by custom URL and add custom fields of profile image like first name, last name in your woo-commerce registration form then see our code below, you can Read more…

Blog

WordPress Create Own Forget Password Page

WordPress Custom Forgot Password Page, we need to use the same page for forgot password as well. Here is the jquery function and also we are going to give id to play with both forms. Read more…

Blog

Best WordPress Navigation Menus

Is it accurate to say that you are searching for the best instructional exercises to work with WordPress route menus? WordPress route menus permit you to effortlessly tweak and oversee menus on your site. In Read more…