how to work with wordpress plugin filters

how to work with wordpress plugin filters

Filters are hooks that WordPress launched to modify text of various types before saving to database. we explained how to work with wordpress plugin filters. Your plugin 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.

how to work with wordpress plugin filters
how to work with wordpress plugin filters

All filters are located in wp-includes/plugin.php file. Following function you can use for using the filters in wordpress plugin.

has filter

Check if any filter has been registered for a hook.

<!--?php has_filters( $tag, $function_to_check ); ?--> 

$tag

(string) (required) The name of the filter hook.

Default: None

$function_to_check
(callback) (optional) If specified, return the priority of that function on this hook or false if not attached.

Default: False

add filter

add_filter( $tag, $function_to_add, $priority, $accepted_args );

$tag
(string) (required) The name of the filter to hook the $function_to_add to.

Default: None

$function_to_add
(callback) (required) The name of the function to be called when the filter is applied.

Default: None

$priority
(integer) (optional) Used to specify the order in which the functions associated with a particular action are executed. Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order in which they were added to the action.

Default: 10

$accepted_args
(integer) (optional) The number of arguments the function(s) accept(s). In WordPress 1.5.1 and newer. hooked functions can take extra arguments that are set when the matching do_action() or apply_filters() call is run.

Default: 1

example

add_filter('media_upload_newtab', array(&$this, 'media_upload_mycallback')); 

apply filters

apply_filters( $tag, $value ); 

$tag
(string) (required) The name of the filter hook.

Default: None

$value
(mixed) (required) The value which the filters hooked to $tag may modify.

Default: None

example.

$myvar = apply_filters( $tag, $value );

current filter

Retrieve the name of the current filter or action.

<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php current_filter() ?>

merge filters

Merge the filter functions of a specific filter hook with generic filter functions.

merge_filters($tag);

$tag
(string) (required) The filter hook of which the functions should be merged.

Default: None

remove filter

This function removes a function attached to a specified filter hook. This method can be used to remove default functions attached to a specific filter hook and possibly replace them with a substitute. See also remove_action(), add_filter() and add_action().
Important: Because of long-time unfixed bugs resulting in flaws of the underlying implementation, it is unpredictable which filters this function will remove when called. Usage might/will result in loss of other then the intended filter(s). Plugin authors should prevent the usage of this function if possible.

Important: To remove a hook, the $function_to_remove and $priority arguments must match when the hook was added. This goes for both filters and actions. No warning will be given on removal failure.

$tag
(string) (required) The action hook to which the function to be removed is hooked.

Default: None

$function_to_remove
(string) (required) The name of the function which should be removed.

Default: None

$priority
(int) (optional) The priority of the function (as defined when the function was originally hooked).

Default: 10

$accepted_args
(int) (optional) The number of arguments the function accepts.

Default: 1

remove all filters

Remove all of the hooks from a filter.

 remove_all_filters( $tag, $priority );

$tag
(string) (required) The filter to remove hooks from.

Default: None

$priority
(integer) (optional) The priority number to remove.

Default: false

——————————————-x———————————–x—————————–

All List of all WordPress filter hooks you will find on following URL

http://adambrown.info/p/wp_hooks/hook/filters

Published by

Purab

I am Purab from India, Software development is my profession and teaching is my passion. Programmers blog dedicated to the JAVA, Python, PHP, DevOps and Opensource Frameworks. Purab's Github Repo Youtube Chanel Video Tutorials Connect to on LinkedIn

Leave a Reply

Your email address will not be published.