Follow

WordPress Filters Explained

Filters are the hooks that WordPress launches to modify text of various types before adding it to the database or sending it to the browser screen. Plugins can specify that one or more of its PHP functions is executed to modify specific types of text at these times, using the Filter API. Pugpig has several built in filters. For a full list of Pugpig Filter Hooks see Pugpig Hooks.

We've got two companion functions:

add_filter()

<?php add_filter( $tag, $function_to_add, $priority, $accepted_args ); ?>

where $tag is the name of the filter to hook the $function_to_add to, and $function_to_add is a callback for the function to be called when the filter is applied.

apply_filters()

<?php apply_filters( $tag, $value, $var ... ); ?>

where $tag is the name of the filter hook, $value is the value which the filters hooked to $tag may modify, and $var is any additional variables passed to the filter functions. 

Example

In pugpig_ad_bundles.php we find

add_filter('pugpig_flatplan_style', 'customapp_pugpig_flatplan_style', 10, 2);
function customapp_pugpig_flatplan_style($output, $post) {
  if ($post->post_type != PUGPIG_AD_BUNDLE_POST_TYPE) return $output;
return $output . "color: green;"; }

The filter is applied in pugpig_admin.php:

function pugpig_get_flatplan_style($post) {
  $style = "font-weight: bold;";
 
  // Allow modules to change the title
  $style = apply_filters('pugpig_flatplan_style', $style, $post);
 
  return $style;
}

This function is ultimately used to define the flatplan editing interface in pugpig_admin.php.

Was this article helpful?
0 out of 0 found this helpful
Have more questions? Submit a request

Comments

Powered by Zendesk