Skip to content

Hooks and filters

__

Action Hooks

Using hooks (https://developer.wordpress.org/plugins/hooks/), you can insert your own code snippets at specific points in the CommonsBooking templates. This allows you to add your own code to the templates without having to replace the template files.

Code snippets are usually very short pieces of PHP code that can be included via a Child Theme or through special code snippet plugins (e.g. Code Snippets). No advanced PHP knowledge is required, it is however also possible to use these snippets to deeply interfere with the functionality of CommonsBooking or even to make the booking system unusable. If you see examples in the documentation, these are reasonably safe and tested. However, a certain residual risk remains. If you encounter problems, please feel free to contact us. However, please also provide all code snippets you are using. This will help us to better understand the problem.

Action hooks are patterned according to the principle

commonsbooking_(before/after)_(template-file)

Using add_action you can integrate your own callback function. Example:

php
function itemsingle_callback() {
    // what should appear before the item single template
}
add_action( 'commonsbooking_before_item-single', 'itemsingle_callback' );

Overview of all of the action hooks:

  • commonsbooking_before_booking-single
  • commonsbooking_after_booking-single
  • commonsbooking_before_location-calendar-header
  • commonsbooking_after_location-calendar-header
  • commonsbooking_before_item-calendar-header
  • commonsbooking_after_item-calendar-header
  • commonsbooking_before_location-single
  • commonsbooking_after_location-single
  • commonsbooking_before_timeframe-calendar
  • commonsbooking_after_timeframe-calendar
  • commonsbooking_before_item-single
  • commonsbooking_after_item-single
  • commonsbooking_mail_sent

Hooks in the context of an object (since 2.10.8)

Some action hooks also additionally pass the post ID of the current object and an instance of the object as a \CommonsBooking\Model<object class> object. Those are:

  • commonsbooking_before_booking-single and commonsbooking_after_booking-single
    • Parameters: int $booking_id, \CommonsBooking\Model\Booking $booking
  • commonsbooking_before_location-single and commonsbooking_after_location-single
    • Parameters: int $location_id, \CommonsBooking\Model\Location $location
  • commonsbooking_before_item-single and commonsbooking_after_item-single
    • Parameters: int $item_id, \CommonsBooking\Model\Item $item
  • commonsbooking_before_item-calendar-header and commonsbooking_after_item-calendar-header
    • Parameters: int $item_id, \CommonsBooking\Model\Item $item
  • commonsbooking_before_location-calendar-header and commonsbooking_after_location-calendar-header
    • Parameters: int $location_id, \CommonsBooking\Model\Location $location

Example usage:

php
function my_cb_before_booking_single( $booking_id, $booking ) {
    echo 'Booking ID: ' . $booking_id;
    echo 'The booking status is ' . $booking->getStatus();
}
add_action( 'commonsbooking_before_booking-single', 'my_cb_before_booking_single', 10, 2 );

Filter hooks

Filter hooks (https://developer.wordpress.org/plugins/hooks/filters) work just like action hooks, but with the difference that the callback function receives a value, modifies it, and then returns it.

Overview of all filter hooks:

  • commonsbooking_isCurrentUserAdmin
  • commonsbooking_isCurrentUserSubscriber
  • commonsbooking_get_template_part
  • commonsbooking_template_tag
  • commonsbooking_tag_$key_$property
  • commonsbooking_booking_filter
  • commonsbooking_mail_to
  • commonsbooking_mail_subject
  • commonsbooking_mail_body
  • commonsbooking_mail_attachment
  • commonsbooking_disableCache

There are also filter hooks that allow you to add additional user roles akin to the CB Manager that can manage items and locations. Read more: Permission management (not translated yet)

In addition to that, there are filter hooks that allow you to change the default values when creating timeframes. More about that here

Filter Hook: commonsbooking_tag_$key_$property

Example: Overwrite who receives booking e-mails

This filter hook can be used in a staging environment to override who receives booking confirmation e-mails.

php
/**
 * This adds a filter to send all booking confirmations to one email adress.
 */
function mywebsite_cb_return_location_mail( $value ){
    return 'yourname@example.com';
}
add_filter('commonsbooking_tag_cb_location__cb_location_email', 'mywebsite_cb_return_location_mail' );

Filter commonsbooking_mobile_calendar_month_count

Since version 2.10.5

How many months are displayed in the mobile calendar view can be adjusted using this filter.

php
// Sets the mobile calendar view to display 2 month
add_filter('commonsbooking_mobile_calendar_month_count', fn(): int => 2);