Explanation for apply_filters function and its variables

That line is using two different functions that need two separate explanations.

__()

This is a translation function. If the settings are done right, it will translate the first parameter from a list of pre-translated strings. If an installation has a file with a compiled translation for this function to use, it will use it. Of course, the plugin has to package its own translation, hence the second parameter. simplr-reg tells __() that the translation of the string 'Please fill out this form to sign up for this site' should be in the translation file associated with 'simplr-reg' (this is done earlier in the plugin by means of the load_plugin_textdomain() function).

The function then returns the translation. If there is no translation to return (e.g. the current language doesn’t have a compiled translation, the string doesn’t have a compiled translation for that package, etc.), the original input is returned.

So for a WordPress site in English, __( 'This', 'simplr-reg' ) is functionally the same as 'This'. To learn more about l10n (Localization), read up on it in the codex:

http://codex.wordpress.org/I18n_for_WordPress_Developers

apply_filters()

This function allows you to filter values used by the plugin as needed. This is one of the main concepts to grasp as a plugin developer. WordPress is extended through hooks, which are basically access points allowing you to time the execution of your plugin’s actions and/or manipulate information/data that WordPress uses, etc.

To manipulate data such as the snippet of code you’ve asked about, you would use the function add_filter(). Here’s a basic example of how this works:

add_filter( 'simplr-reg-instructions', 'wpse16573_my_filter' );

You’ll probably recognize the first argument there. It’s the same one used in add_filter above. This is the hook name. The second argument is the filter callback. It must be a valid callback to a function (read more about callbacks here). That line of code says “When the 'simplr-reg-instructions' hook is executed, run the function with the callback I provided.” apply_filters() executes the hook found in its first argument, essentially meaning “execute all functions registered for this hook.” apply_filters then passes all other arguments (in this case, 'Please fill out this form to sign up for this site') to the functions on that filter. So, the callback I used above should look like this:

function wpse16573_my_filter( $text ){
  $text = "<strong>$text</strong>";
  return $text;
}

There are two kinds of hooks in WordPress: filters (the kind we’re using here) and actions. The main difference between the two is that filters expect you to return something and actions do not. So, for this filter, my example above adds some html around 'Please fill out this form to sign up for this site' and returns it.

Read more about actions and hooks here:

http://codex.wordpress.org/Plugin_API

Leave a Comment