Custom Filters¶
Custom filters behave just like the default filters. You have to use the curly brackets {{ … }} syntax. Check Default Filters for more details about the usage.
Now it’s getting intresting. Post Status Notifier provides custom filters specialized for the usage with WordPress.
divide¶
The filter divide
divides an array into parts. The number of parts is defined by the argument.
This filter makes it easy to display array data in a column design. The example divides the post categories into three parts and uses two for loops (For loop) to display the result in a three column design.
<div>
{% for part in [post_categories_array]|divide(3) %}
<div style="float: left; width: 33%;"><ul>
{% for column in part %}
<li>{{ column }}</li>
{% endfor %}
</ul></div>
{% endfor %}
<div style="clear: both;"></div>
</div>
get_key¶
You can extract a value from list data (e.g. created by the unserialize filter) by using the custom filter get_key
which takes a list key as parameter and returns its value. get_key
works recursive through multidimensional lists. If the key does not exits an empty string will be returned.
{{ edd_payment_meta|get_key("currency") }}
This will output “USD” from the example data above.
If you just want to use the list data once, you can combine both filters unserialize and get_key in one command:
{{ [post_custom_field-_edd_payment_meta]|unserialize|get_key("currency") }}
unserialize¶
The unserialize filter comes in handy when you are dealing with list data provided by plugins. For example let’s look at a part of the meta data Crowdfunding by Astoundify stores in the custom field “edd_payment_meta” which you can access with PSN’s placeholder [post_custom_field-_edd_payment_meta]:
a:6:{s:8:"currency";s:3:"USD";s:9:"downloads";s:73:"a:1:{i:0;a:3:{s:2:"id";s:2:"96";s:7:"options";a:0:{}s:8:"quantity";i:1;}} ... "
It’s a serialized string of a PHP array which Crowdfunding by Astoundify uses internally to store and retrieve data related to a campaign. It would be great if we could use this information in a notifcation email.
So to use the serialized information you first have to unserialize the string and transform it into a list you can work with.
{{ [post_custom_field-_edd_payment_meta]|unserialize }}
For reusability we can put the list data in a temporary variable, e.g. “edd_payment_meta”, by using the set function.
{% set edd_payment_meta = [post_custom_field-_edd_payment_meta]|unserialize %}