# Hook/plugin to inject front matter header

**URL:** https://talk.jekyllrb.com/t/hook-plugin-to-inject-front-matter-header/1925
**Category:** Help
**Created:** [June 26, 2018, 2:47am UTC](https://talk.jekyllrb.com/t/hook-plugin-to-inject-front-matter-header/1925 "2018-06-26T02:47:23Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![jabowa](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/jabowa/32/1091_2.png) [@jabowa](https://talk.jekyllrb.com/u/jabowa)
#### Post date: [June 26, 2018, 2:47am UTC](https://talk.jekyllrb.com/t/hook-plugin-to-inject-front-matter-header/1925/1 "2018-06-26T02:47:24Z")

</div>

Is it possible to inject/add front matter key/values using a hook for newly created markdown files (posts or custom collections)?

I am aware of the ability to add default header values using defaults in \_config.yml file. However, this does not add those values to the actual front matter header and only allows access in liquid on the page variable.

The reason for this is because we have our pages structured like \_pages/category/sub-category/file.md and we use cloud cannon for non technical users to edit/create content.

I’ve got a proof of concept working using File.write() within a hook but this triggers an infinite regenerate loop when running jekyll serve (I believe its because the file is written and the hook is triggered creating a loop).

I am using `Jekyll::Hooks.register :site, :post_read` to achieve this. Can someone give me advice on how to accomplish this? Ultimately, we want a hook to run for newly created files and do some logic to add the yaml front matter headers for the file.

---

<div class="post-metadata">

### Author: ![ginfuru](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/ginfuru/32/773_2.png) [@ginfuru](https://talk.jekyllrb.com/u/ginfuru)
#### Post date: [June 29, 2018, 11:15am UTC](https://talk.jekyllrb.com/t/hook-plugin-to-inject-front-matter-header/1925/2 "2018-06-29T11:15:54Z")

</div>

Jekyll doesn’t have this feature… Out of the box. But since you are using cloud cannon you can take advantage of their collection defaults

> **[Collection Defaults](https://docs.cloudcannon.com/editing/experience/collection-defaults/)**
>
> Provide default front matter and/or content for new drafts and collection items. Defaults for collection item files are copied from \_defaults.md in the root collection folder. Defaults files are hidden in the Explore section to prevent changes from...

---

<div class="post-metadata">

### Author: ![jabowa](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/jabowa/32/1091_2.png) [@jabowa](https://talk.jekyllrb.com/u/jabowa)
#### Post date: [July 1, 2018, 3:33pm UTC](https://talk.jekyllrb.com/t/hook-plugin-to-inject-front-matter-header/1925/3 "2018-07-01T15:33:57Z")

</div>

Yeah we’re already using cloud cannon defaults feature but the issue is when we have nested folders within a collection. Cloud cannon only allows one defaults.md for a collection so you cannot nest it like so: \_pages/category/sub-category/\_defaults.md, it will only work when you have it within root of the collection folder \_pages/category/\_defaults.md.

We’re looking for a way to categorize “pages” similar to how posts is done out of the box in Jekyll but without the need to make the filename in the date-title.md format. We’re also using posts but its for blogs/news related articles that require date and other blog related attributes.

---

<div class="post-metadata">

### Author: ![ginfuru](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/ginfuru/32/773_2.png) [@ginfuru](https://talk.jekyllrb.com/u/ginfuru)
#### Post date: [July 1, 2018, 7:11pm UTC](https://talk.jekyllrb.com/t/hook-plugin-to-inject-front-matter-header/1925/4 "2018-07-01T19:11:59Z")

</div>

Hmm did you try using the Array defaults? But instead of using `_pages` just create a collection instead?

> **[Adding Options](https://docs.cloudcannon.com/editing/options/options/)**
>
> Configure the various editing interfaces for your clients to optimise for their specific needs. Options can be set for keys matching the same name in three ways:

---

<div class="post-metadata">

### Author: ![jabowa](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/jabowa/32/1091_2.png) [@jabowa](https://talk.jekyllrb.com/u/jabowa)
#### Post date: [July 9, 2018, 2:22pm UTC](https://talk.jekyllrb.com/t/hook-plugin-to-inject-front-matter-header/1925/5 "2018-07-09T14:22:02Z")

</div>

Thanks! Managed to figure this out using a custom plugin that adds category and sub category to the front matter header based on the directory path.

---

<div class="post-metadata">

### Author: ![plllmg](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/plllmg/32/1122_2.png) [@plllmg](https://talk.jekyllrb.com/u/plllmg)
#### Post date: [July 23, 2018, 3:18pm UTC](https://talk.jekyllrb.com/t/hook-plugin-to-inject-front-matter-header/1925/6 "2018-07-23T15:18:11Z")

</div>

@jabowa could I take a look at that custom plugin?

I’m facing a similar issue where I want to dynamically inject an array of category tags to a large number of pages.

---

<div class="post-metadata">

### Author: ![jabowa](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/jabowa/32/1091_2.png) [@jabowa](https://talk.jekyllrb.com/u/jabowa)
#### Post date: [July 24, 2018, 8:39pm UTC](https://talk.jekyllrb.com/t/hook-plugin-to-inject-front-matter-header/1925/7 "2018-07-24T20:39:22Z")

</div>

@plllmg [https://gist.github.com/bsubedi26/eb41c3c359129dd965780592128f4eb9](https://gist.github.com/bsubedi26/eb41c3c359129dd965780592128f4eb9)

Make sure you specify which collections to dynamically add category/sub category to their front matter headers in \_data/categorize\_collections.yml or it defaults to pages collection.

---

<div class="post-metadata">

### Author: ![plllmg](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/plllmg/32/1122_2.png) [@plllmg](https://talk.jekyllrb.com/u/plllmg)
#### Post date: [July 26, 2018, 7:41pm UTC](https://talk.jekyllrb.com/t/hook-plugin-to-inject-front-matter-header/1925/8 "2018-07-26T19:41:04Z")

</div>

@jabowa Thanks so much!
