Contribute to Docs Contribute to Docs

Hooks

Hooks is a simple way to change, or extend the default behaviour of Piranha CMS. Hooks can either extend the default behaviour or replace a behaviour depending on the hook. Hooks should be added in the Configure method in your application startup after Piranha.App has been initialized.

General

OnGenerateSitemap

App.Hooks.OnGenerateSitemap += (sitemap) =>
{
    // Add custom items to the sitemap before rendering Sitemap.xml
}

This hook extends the built in Sitemap generation and can be used for adding custom entries, when you for example have pages in your site that is not controlled by Piranha CMS.

OnGenerateSlug

App.Hooks.OnGenerateSlug += (str) =>
{
    var slug = "";

    // Generate custom slug from input string

    return slug;
}

This hook replaces the default slug generation and can be used when you for example want to support different charsets than the default slug generation. It is executed when a model with a slug (page, post, category & tag) is added and updated.

Alias

The following hooks are available when accessing aliases through the AliasService. All of these hooks can be used to extend the default behaviour of the service layer.

App.Hooks.Alias.RegisterOnLoad((alias) => { ... });

App.Hooks.Alias.RegisterOnBeforeSave((alias) => { ... });
App.Hooks.Alias.RegisterOnAfterSave((alias) => { ... });

App.Hooks.Alias.RegisterOnBeforeDelete((alias) => { ... });
App.Hooks.Alias.RegisterOnAfterDelete((alias) => { ... });

Comments

The following hooks are available when accessing comments through the PageService or PostService. All of these hooks can be used to extend the default behaviour of the service layer. Please note that the same hooks will be called regardless of what content the comment is related to.

App.Hooks.Comments.RegisterOnLoad((comment) => { ... });

App.Hooks.Comments.RegisterOnBeforeSave((comment) => { ... });
App.Hooks.Comments.RegisterOnAfterSave((comment) => { ... });

App.Hooks.Comments.RegisterOnBeforeDelete((comment) => { ... });
App.Hooks.Comments.RegisterOnAfterDelete((comment) => { ... });

App.Hooks.Comments.RegisterOnValidate((comment) => { ... });

Media

The following hooks are available when accessing media assets through the MediaService. All of these hooks can be used to extend the default behaviour of the service layer.

App.Hooks.Media.RegisterOnLoad((media) => { ... });

App.Hooks.Media.RegisterOnBeforeSave((media) => { ... });
App.Hooks.Media.RegisterOnAfterSave((media) => { ... });

App.Hooks.Media.RegisterOnBeforeDelete((media) => { ... });
App.Hooks.Media.RegisterOnAfterDelete((media) => { ... });

Media Folders

The following hooks are available when accessing folders through the MediaService. All of these hooks can be used to extend the default behaviour of the service layer.

App.Hooks.MediaFolder.RegisterOnLoad((folder) => { ... });

App.Hooks.MediaFolder.RegisterOnBeforeSave((folder) => { ... });
App.Hooks.MediaFolder.RegisterOnAfterSave((folder) => { ... });

App.Hooks.MediaFolder.RegisterOnBeforeDelete((folder) => { ... });
App.Hooks.MediaFolder.RegisterOnAfterDelete((folder) => { ... });

Pages

The following hooks are available when accessing pages through the PageService. Please note that all models are passed in as Piranha.Models.PageBase and must be casted to the applicable type.

App.Hooks.Pages.RegisterOnLoad((page) => { ... });

App.Hooks.Pages.RegisterOnBeforeSave((page) => { ... });
App.Hooks.Pages.RegisterOnAfterSave((page) => { ... });

App.Hooks.Pages.RegisterOnBeforeDelete((page) => { ... });
App.Hooks.Pages.RegisterOnAfterDelete((page) => { ... });

Params

The following hooks are available when accessing params through the ParamService. All of these hooks can be used to extend the default behaviour of the service layer.

App.Hooks.Param.RegisterOnLoad((param) => { ... });

App.Hooks.Param.RegisterOnBeforeSave((param) => { ... });
App.Hooks.Param.RegisterOnAfterSave((param) => { ... });

App.Hooks.Param.RegisterOnBeforeDelete((param) => { ... });
App.Hooks.Param.RegisterOnAfterDelete((param) => { ... });

Posts

The following hooks are available when accessing posts through the PostService. Please note that all models are passed in as Piranha.Models.PostBase and must be casted to the applicable type.

App.Hooks.Post.RegisterOnLoad((post) => { ... });

App.Hooks.Post.RegisterOnBeforeSave((post) => { ... });
App.Hooks.Post.RegisterOnAfterSave((post) => { ... });

App.Hooks.Post.RegisterOnBeforeDelete((post) => { ... });
App.Hooks.Post.RegisterOnAfterDelete((post) => { ... });

Sites

The following hooks are available when accessing sites through the SiteService. All of these hooks can be used to extend the default behaviour of the service layer.

App.Hooks.Site.RegisterOnLoad((site) => { ... });

App.Hooks.Site.RegisterOnBeforeSave((site) => { ... });
App.Hooks.Site.RegisterOnAfterSave((site) => { ... });

App.Hooks.Site.RegisterOnBeforeDelete((site) => { ... });
App.Hooks.Site.RegisterOnAfterDelete((site) => { ... });

Site Content

The following hooks are available when accessing site content through the SiteService. Please note that all models are passed in as Piranha.Models.SiteContentBase and must be casted to the applicable type.

App.Hooks.SiteContent.RegisterOnLoad((siteContent) => { ... });

App.Hooks.SiteContent.RegisterOnBeforeSave((siteContent) => { ... });
App.Hooks.SiteContent.RegisterOnAfterSave((siteContent) => { ... });

App.Hooks.SiteContent.RegisterOnBeforeDelete((siteContent) => { ... });
App.Hooks.SiteContent.RegisterOnAfterDelete((siteContent) => { ... });