<p>When inserting a link, CKEditor gives you the option between selecting an element (for example, an entry or category), or entering a URL manually.</p>
<p>Your users will be able to create links in the above format by manually entering the URL – but AFAIK there is no way out of the box to select an element <em>and</em> have the link's URL render as per a custom format.</p>
<p>You might be able to pull it off by writing a <a href="https://craftcms.com/docs/5.x/extend/extending-twig.html#create-a-twig-extension" rel="nofollow noreferrer">custom Twig extension</a> to rewrite the <a href="https://craftcms.com/docs/5.x/system/reference-tags.html" rel="nofollow noreferrer">reference tags</a> Craft adds to the raw CKEditor markup. Your categories will need to have URLs enabled to be available for selection in the editor in the first place, though.</p>
<p><strong>A more straight-forward solve</strong> could be to give your category group (or section, depending on what you're actually using for your &quot;categories&quot;) the following <a href="https://craftcms.com/docs/5.x/reference/element-types/entries.html#entry-uri-formats" rel="nofollow noreferrer">URI format</a>:</p>
<pre><code>blog#{id}
</code></pre>
<p>...or</p>
<pre><code>blog#{slug}
</code></pre>
<p>Or whatever format you need.</p>
<p>Considering the fact that your categories will need a URI format regardless, in order to be selectable as links in CKEditor fields, making Craft generate their URLs in the desired format to begin with could be an effective solution that requires no further hacks or custom code on your end.</p>

CC BY-SA 4.0
2024-11-11T21:57:16.533Z
Thanks Mats. I want to create 'prefiltered' links like /blog#libraries where libraries is a category slug. The reason for this format is we're using a slug in the URL to create on-the-fly AJAX filtering at that URL.
4midori
CC BY-SA 4.0
2024-11-11T22:39:44.233Z
@4midori Gotcha. I just edited my answer with a potential solution that would require no custom code on your end. Might not be feasible if you need the categories to have "real" URLs in addition to these "prefiltered" links, but basically letting Craft do the work w/ the URI format in the first place would be a simple way to solve it.
Mats Mikkel Rummelhoff
CC BY-SA 4.0
2024-11-13T02:35:30.950Z
Thanks - this could work!
4midori
CC BY-SA 4.0
2024-11-09T21:50:28.727Z
2024-11-11T22:38:02.470Z
2024-11-11T22:38:02.470Z
Mats Mikkel Rummelhoff
<p>I've got multi sites under one Craft install I'll be moving from 4.x to 5.x. I'm familiar with CKEditor, but am wondering about a specific feature.</p>
<p>What if I need CKEditor to build links in a specific format, like:</p>
<pre><code>/blog#3892
</code></pre>
<p>where 3892 is a category ID? And yes, the # is correct. In this case, the user would choose a single category in a category group I specify. Can this be done within the editor without writing a plugin or getting hacky?</p>

<categories><craft5><custom><ckeditor>
CKEditor and building custom links
# CKEditor and building custom links

(Asked by: username_0 on 2024-11-07)

I've got multi sites under one Craft install I'll be moving from 4.x to 5.x. I'm familiar with CKEditor, but am wondering about a specific feature.
What if I need CKEditor to build links in a specific format, like:
/blog#3892

where 3892 is a category ID? And yes, the # is correct. In this case, the user would choose a single category in a category group I specify. Can this be done within the editor without writing a plugin or getting hacky?

**Tags:** categories, craft5, custom, ckeditor

---

## 1 answers

### Answer by username_1 on 2024-11-09. Score: 1 | Accepted answer

When inserting a link, CKEditor gives you the option between selecting an element (for example, an entry or category), or entering a URL manually.
Your users will be able to create links in the above format by manually entering the URL – but AFAIK there is no way out of the box to select an element and have the link's URL render as per a custom format.
You might be able to pull it off by writing a custom Twig extension to rewrite the reference tags Craft adds to the raw CKEditor markup. Your categories will need to have URLs enabled to be available for selection in the editor in the first place, though.
A more straight-forward solve could be to give your category group (or section, depending on what you're actually using for your "categories") the following URI format:
blog#{id}

...or
blog#{slug}

Or whatever format you need.
Considering the fact that your categories will need a URI format regardless, in order to be selectable as links in CKEditor fields, making Craft generate their URLs in the desired format to begin with could be an effective solution that requires no further hacks or custom code on your end.

> **Comments:**
> **username_0**: Thanks Mats. I want to create 'prefiltered' links like /blog#libraries where libraries is a category slug. The reason for this format is we're using a slug in the URL to create on-the-fly AJAX filtering at that URL.  
> **username_1**: @4midori Gotcha. I just edited my answer with a potential solution that would require no custom code on your end. Might not be feasible if you need the categories to have "real" URLs in addition to these "prefiltered" links, but basically letting Craft do the work w/ the URI format in the first place would be a simple way to solve it.  
> **username_0**: Thanks - this could work!  

---

