V
V
vitovt2016-03-17 15:59:46
WordPress
vitovt, 2016-03-17 15:59:46

How do events and filters work in Wordpress?

Good afternoon!
I can’t understand the essence of filters in Wordpress . I
delve into the functions and see, for example, this:

function wp_spaces_regexp() {
  static $spaces;

  if ( empty( $spaces ) ) {
    /**
     * Filter the regexp for common whitespace characters.
     *
     * This string is substituted for the \s sequence as needed in regular
     * expressions. For websites not written in English, different characters
     * may represent whitespace. For websites not encoded in UTF-8, the 0xC2 0xA0
     * sequence may not be in use.
     *
     * @since 4.0.0
     *
     * @param string $spaces Regexp pattern for matching common whitespace characters.
     */
    $spaces = apply_filters( 'wp_spaces_regexp', '[\r\n\t ]|\xC2\xA0| ' );
  }

  return $spaces;

That is, there is a function that does not take any parameters, but only returns.
Inside the function, apply_filters is triggered, which calls the same function with some parameter? As I understand it, the filter hangs on the performance of some function and adds actions? But the wp_spaces_regexp function is the same in all Wordpress code

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor Vorotnev, 2016-03-17
@HeadOnFire

You can attach your function to this filter from the plugin / theme and specify your regular expression (in this particular case). If WordPress sees your connected function in this place, it will use your regular expression (or other functionality), if not, its standard one.
In short, there are 2 types of hooks - action and filter.
Action is an action. Roughly, this is a kind of outlet at a certain point in the execution of the WP code. If you connected a function with your own functionality to this socket, it will be executed at that moment. Many functions can be connected to one outlet, you can set priority for them.
Filter is work with a portion of content at a particular moment. By connecting to the filter, you get a piece of content at the input, you can do anything with it and return it changed from the function back. There can also be many of them, with different priorities.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question