Goals and background
- This documents how to extend CiviCRM to meet your needs. CiviCRM uses hooks in a very similar manner to Drupal, primarily because based on our experience with Drupal's extension architecture, we think it is clean and non-intrusive, yet incredibly powerful. For a simple example module check civitest module
- See Drupal hook documentation for a description of how hooks are implemented.
- See CRM/Utils/Hook.php in CiviCRM for the source code that invokes these hooks.
- Hooks can be implemented within a module or extension. In general, implement a hook by declaring a global function that starts with the name of your module and ends with the name of the hook.
For example: to implement hook_civicrm_buildForm from within the "my_custom" module/extension you would add the following function to your main .php or .module file (or a file always included by that script):
As long as the module/extension is enabled, this function will be called every time CiviCRM builds a form.
For A CiviCRM (native) Extension
For A Drupal Module
For a Joomla Plugin
- See this page for creating a Joomla plugin. Joomla plugins implement the observer design pattern.
- Hook plugins should be placed in the civicrm plugin group in a subfolder with the same name as the plugin.
- Once created plugins may be packaged and installed with the Joomla installer or the files can be placed in the appropriate folder and installed with the discover method.
- See this sample Joomla plugin for CiviCRM hooks
For a Wordpress Plugin
- Wordpress can use hooks defined by a custom php file placed in the custom php path (as specified in Administer -> System Settings -> Directories -> Custom PHP Path Directory)
- The custom php file works very similarly to your wordpress
functions.php file. Just start the file with
<?php and start dropping in your functions (don't forget to end it with
?>). The php file containing the hooks must be called
civicrmHooks.php so that it gets loaded (parsed) by CiviCRM.
- Hooks should start with "wordpress_" instead of "hook_" so to implement hook_civicrm_pre you would write: function wordpress_civicrm_pre($op, objectName, $objectId, &$objectRef)
- To package hooks for multiple wordpress sites, consider writing a CiviCRM (native) extension.
The documentation about hooks can be somewhat abstract, and it sometimes helps to see interactively how the hooks run. If you use Drupal, then you can inspect some hooks by installing these two Drupal modules: