In the third part of my series about PodsCMS 2.x I would like to write a little about new action hooks which were introduced recently. Previously we used a small number of helper functions which were a bit limited when it comes to possible usage cases, their debug and development wasn’t too clear also. Now the things are different.

In 2.x the amount of events where we can hook in has greatly increased in comparison to PodsCMS 1.x. Another good news is that  hooks are duplicated for each action – first hook is fired for all types of pods, second one is fired just for particular type of pod – which gives us a lot of possible usage scenarios. We can target our functions to all pods or maybe just for the ones which are freshly created and belong to particular pod type.

!!! Please remember – all hook names are prefixed with ‘pods_{$scope}_ ‘ string so we have use for example “pods_api_pre_save_pod_item” to access our filter !!!

Pod pre-save (scope = api)

Those hooks are called before we save our pod data. We can use them to do some calculations, set values for hidden fields or validate data. They are also very useful if we want to fire specific functions depending on value changes (increase, decrease, relationship changes).

  • pre_save_pod_item (ex. pods_api_pre_save_pod_item)
  • pre_save_pod_item_{podname} (ex. pods_api_pre_save_pod_item_books)
  • pre_create_pod_item (ex. pods_api_pre_create_pod_item)
  • pre_create_pod_item_{podname} (ex. pods_api_pre_create_pod_item_books)
  • pre_edit_pod_item (ex. pods_api_pre_edit_pod_item)
  • pre_edit_pod_item_{podname} (ex. pods_api_pre_edit_pod_item_books)

Pod post-save (scope = api)

Those hooks are called after we save our pod item. We can use it for various tasks like email notifications for example. I am using them very heavily, mostly for statistical purposes.

  • post_save_pod_item
  • post_save_pod_item_{podname}
  • post_create_pod_item
  • post_create_pod_item_{podname}
  • post_edit_pod_item
  • post_edit_pod_item_{podname}

 Pod pre-delete (scope = api)

Those actions are performed just right before pod item is being deleted.

  • pre_delete_pod_item
  • pre_delete_pod_item_{podname}

 Pod post-delete (scope = api)

Finally, post delete actions are very useful for any update scripts, cleaning functions (similar to post-save ones).

  • post_delete_pod_item
  • post_delete_pod_item_{podname}

Development with easy debugging and sample example

As I mentioned in one of previous articles debugging pod item helpers in podsCMS 1.x was a hard thing mainly because pods data was saved using AJAX calls. Now pods are saved in similar way as WordPress posts – using post-redirect-get method.

Why am I writing about it? Well that’s very important for us because hooks are fired before redirect call is made and all data is not available for us – so simply relying on var_dump() or WP_DEBUG will not work here.

Fortunately this is not a problem and we have various possibilities to solve that problem. Please let me present one way of achieving this task.

I always put pods functionality in separate plugin to have an easy possibility to use pods functionalities on various sites. For our purposes lets just assume that we would like to do it on a theme level.

1. Create file podsDebugger.php in your themes main directory

2. Open functions.php and add at the bottom

Please change TEMPLATEPATH to STYLESHEETPATH if You are using child theme.

3. Now we are ready to develop and debug our hook functions. Open functions.php once again and add:

Now You should see nice array of variables which You can use.

And what now? Well that’s up to you 🙂

Related posts

Posted by Kamil Grzegorczyk Owner / Development