<p>I think for this you'd want to write a <a href="https://craftcms.com/docs/plugins/events-reference#entries-onSaveEntry" rel="nofollow noreferrer">custom plugin</a> that listened to the <a href="https://craftcms.com/docs/plugins/events-reference#entries-onSaveEntry" rel="nofollow noreferrer"><code>onSaveEntry</code></a> event.</p>

<p>That plugin would check that the given entry is in the correct section/entry type you're interested in, pull any data from custom fields set on the entry update the slug for that entry accordingly.</p>

<p>I'd probably look at updating the slug directly in the database and avoid going back through <code>craft()-&gt;entries-&gt;saveEntry()</code> to avoid getting into an infinite save entry loop.</p>

CC BY-SA 4.0
2019-06-21T22:28:59.513Z
Old answer, I know, but is this something I could do easily enough with a module? - trying my hand at learning how to do it.
Jay
CC BY-SA 3.0
2017-06-15T22:56:16.517Z
2017-06-15T22:56:16.517Z
Brad Bell
<p>I'm not sure I've seen this exact question, but I'm curious how I can include custom and/or auto-generated parts of a slug. It's not the URI creation, but the slug itself.</p>

<p>For Instance:</p>

<p><code>{slug}-{id}</code> would turn into <code>lorem-ipsum-dolor-sit-amet-123456</code> on submit</p>

<p>and from there on the variable <code>{{entry.url}}</code> would be <code>example.com/lorem-ipsum-dolor-sit-amet-123456</code>.</p>

<p>I know ExpressionEngine had an option in the Channel preferences called URL Title Prefix, so this would go along those lines, but more auto-generated than a predefined set of words.</p>

<p>If it changes things, this is specifically for Commerce and Digital Products at the moment.</p>

CC BY-SA 3.0
2017-06-15T18:54:03.700Z
You can do so with Craft's [Dynamic Entry Titles](https://craftcms.stackexchange.com/questions/20857/auto-generating-slug-with-custom-fields-or-id)
AbbeyDesign
CC BY-SA 3.0
2017-06-15T19:25:27.213Z
https://craftcms.com/docs/templating/filters#kebab
Brad
CC BY-SA 3.0
2017-06-15T20:30:40.843Z
I don't necessarily want the title to be dynamic. The title should be able to be populated by the user. But upon submit, or even right away when the create entry page loads, the slug should be prepended or appended by the ID.

@Brad I don't know how that kebab filter helps?
John Johnson
CC BY-SA 3.0
2017-06-16T12:56:33.407Z
Dynamic Entry Titles are dynamic in the sense saving an entry would create a slug depending on any combination of fields you want. You can set your title field to be {entry.user-entered-slug}-{entry.id} or {entry.fieldA}-{entry.fieldB}-{entry.postDate}-some-text-{entry.id} etc etc.
AbbeyDesign
CC BY-SA 3.0
2017-06-16T13:26:13.173Z
OK, I think I'm following. I could create a psuedo title field and behind the scenes it's populating the actual title field with the pertinent info I need. I'll try this...
John Johnson
CC BY-SA 3.0
2017-06-16T13:29:31.170Z
Upon looking, it doesn't appear that Craft Digital Products has the ability to hide the title field, so this method is not possible in the context I need it.
John Johnson
<slug>
Auto generating slug with custom fields or ID
# Auto generating slug with custom fields or ID

(Asked by: username_0 on 2017-06-15)

I'm not sure I've seen this exact question, but I'm curious how I can include custom and/or auto-generated parts of a slug. It's not the URI creation, but the slug itself.

For Instance:

`{slug}-{id}` would turn into `lorem-ipsum-dolor-sit-amet-123456` on submit

and from there on the variable `{{entry.url}}` would be `example.com/lorem-ipsum-dolor-sit-amet-123456`.

I know ExpressionEngine had an option in the Channel preferences called URL Title Prefix, so this would go along those lines, but more auto-generated than a predefined set of words.

If it changes things, this is specifically for Commerce and Digital Products at the moment.

**Tags:** slug
> **Comments:**
> **username_1**: You can do so with Craft's [Dynamic Entry Titles](https://craftcms.stackexchange.com/questions/20857/auto-generating-slug-with-custom-fields-or-id)  
> **username_2**: https://craftcms.com/docs/templating/filters#kebab  
> **username_0**: I don't necessarily want the title to be dynamic. The title should be able to be populated by the user. But upon submit, or even right away when the create entry page loads, the slug should be prepended or appended by the ID.  @Brad I don't know how that kebab filter helps?  
> **username_1**: Dynamic Entry Titles are dynamic in the sense saving an entry would create a slug depending on any combination of fields you want. You can set your title field to be {entry.user-entered-slug}-{entry.id} or {entry.fieldA}-{entry.fieldB}-{entry.postDate}-some-text-{entry.id} etc etc.  
> **username_0**: OK, I think I'm following. I could create a psuedo title field and behind the scenes it's populating the actual title field with the pertinent info I need. I'll try this...  
> **username_0**: Upon looking, it doesn't appear that Craft Digital Products has the ability to hide the title field, so this method is not possible in the context I need it.  

---

## 1 answers

### Answer by username_3 on 2017-06-15. Score: 2

I think for this you'd want to write a [custom plugin](https://craftcms.com/docs/plugins/events-reference#entries-onSaveEntry) that listened to the [`onSaveEntry`](https://craftcms.com/docs/plugins/events-reference#entries-onSaveEntry) event.

That plugin would check that the given entry is in the correct section/entry type you're interested in, pull any data from custom fields set on the entry update the slug for that entry accordingly.

I'd probably look at updating the slug directly in the database and avoid going back through `craft()->entries->saveEntry()` to avoid getting into an infinite save entry loop.

> **Comments:**
> **username_4**: Old answer, I know, but is this something I could do easily enough with a module? - trying my hand at learning how to do it.  

---

