A few words of introduction
Pods framework is a great framework for WordPress built by Scott Kingsley Clark and his team. You can find the plugin in WordPress.org directory or directly on projects website – podsframework.org.
At LowGravity I have used this plugin for various purposes – from small widgets to enhanced statistics system and various directories. You can see few examples here:
- HaynesHouse.com – frontpage statistics widget,
- KeydrenClark.com, T2Hosley.com – full statistics system gathering a data from their whole career, frontpage statistics widget,
- NetscoutsBasketball.com – scouting reports database.
Recently there was a big change made to pods framework – plugin was totally rewritten and finally, after long wait, I was able to lay my hands on fresh new Pods 2.0 version. There is a lot of changes under the hood, a lot of new functionalities, some of previous features are missing (like packages), but the guys are working very hard to bring them back.
Unfortunately writing documentation for such complicated system is long process and still few crucial areas are not documented (yet 😉 ). That is why I have decided to write that short article while I’m working on rewriting few plugins developed during my last 2 years with pods framework
Embrace 2.x action hooks – ancestor of 1.x helpers
In version 1.x pods have used helper functions in order to achieve various tasks. We were able to fire functions on various events like pod save, drop or field input. Now things are similar but helpers are deprecated and action hooks come into play.
I wasn’t a big fan of 1.x helpers because of the way they were handled (they were ran through eval() function, debugging save helpers was a pain in the a**) – now the situation is totally different and I consider action hooks as a very powerful tool.
So now lets divide those actions to two categories:
- field related actions
- pod item related actions
Today I’ll write about few filters from first category. Filters related to pod items will be described in second part of this tutorial.
Field input
In class PodsForm.php we can find various filters which we can use in our projects. Unfortunately there is not enough space to describe them all in this short article so I would like to explain two most important ones ( at least for now ).
- pods_form_ui_field_{$type}_value – useful if we want to set default value or do some other operations.
- pods_form_ui_field_{$type} (ex. pods_form_ui_field_text for text input) – changing the default html ouput in forms for particular field type
Usage
We can use those filters as input helpers known from 1.x version.
Simple select field
function test_input_helper ($output, $name, $value, $options, $pod, $id) {
//defining few defaults
//list of choices for our dropdown
$choices = array(
'first' => 'My first choice',
'second' => 'My second choice',
'other' => 'Some other value',
);
//name of the field we would like to replace
$field_name = 'field_name';
//as this filter is assigned to each text field we need to filter the ones we need
if(strstr($name, $field_name )) {
//we need to assign choices list to our dropdown field
$options['data'] = $choices;
//pods already have all the functionality for select fields
pods_view( PODS_DIR . 'ui/fields/select.php', compact( array_keys( get_defined_vars() ) ) );
return;
} else {
//for all other fields just return default output
return $output;
}
}
//now we assign our function to the filter
add_filter('pods_form_ui_field_text', 'test_input_helper', 10, 6);
Setting default value for our dropdown
function test_input_helper_default($value, $name, $options, $pod, $id) {
$field_name = 'my_field_name';
if(strstr($name, $field_name )) {
if(empty($value)) {
return $value = 'other';
}
} else {
return $value;
}
}
add_filter('pods_form_ui_field_text_value', 'test_input_helper_default', 10, 5);
Of course those two snippets above can be extended and optimized and we are not limited just to select fields (which probably will be introduced in pods 2.x anyway – now You can use special variant of relationship field) like google maps fields and many other fields needed in Your projects.
Other field input hooks You can use:
- pods_form_ui_label_text
- pods_form_ui_label_help
- pods_form_ui_comment_text
- pods_form_ui_field_{$type}_data – generates “data” attributes (if You want to play with jQuery a little)
- pods_field_{$type}_validate – for custom validation rules
- pods_form_field_permission – field access permissions
- and many other.
Field output
Currently there aren’t many display hooks in pods framework code. I’m a big fan of working on a raw data but as soon as there will be any info about that I will edit that section or write new part of article.
Summary
I’m very impressed about the new direction of pods framework plugin. I’m awaiting for all upcoming fetaures and I hope that they will restore packages functionality. I would also like to see an easy way of registering new field types (which will make input filters less important but also give us more power) – similar way as it is done in brilliant plugin advanced custom fields by Elliot Condon. Some of the code for that feature is already implemented, I haven’t tested it yet.
That is all… for now :). In next parts I will try to get deeper into the topic.
If You have any questions or requests please just leave a comment below!