Filters in Dynamic Shortcodes
Filters allows you to transform the Dynamic Shortcode output to get what you need. The filters work as pipeline, they take a value they do something with it and then pass the result to the next filter or, if no more filters are present, as the final result of the shortcode. There can be arbitrarily many chained filters inside a shortcode.
Identity Dynamic Shortcodes
Note: In the following the identity Dynamic Shortcode is the most simple shortcode, used mainly for testing, it takes the value in the first argument and returns as it is. For example {identity:3}
will return 3
Types of Filters
There are three filters:
Pipe First (|)
The pipe first (symbol |), will pass the value coming from the left to the given function as the first argument, the other arguments given inside parentheses will be passed to the function after that. For example {identity:"hello world"|strtoupper}
will be expanded to "HELLO WORLD!". Another example is: {acf:myfield|my_function(arg1)}
The value of the field will be passed to the custom function together with arg1, if $myfield_value is the value of the ACF field, the function is called like this: my_function($my_field_value, "arg1")
Pipe Last (|-)
The pipe last (symbol |-, a pipe and minus) work like the pipe first filter, but the value coming from the left is always passed as the last argument. For example {identity:hello|-str_replace(e, a)}
will expand to hallo
.
Access (||)
The access filter (symbol ||) is used for accessing value inside arrays. It can be used both with numeric and string keys. For example {array:a b c||1}
will expand to b. And {array:@a=1 b=2 c=3||b}
will expand to 2
.