<p>How about just using the replace filter?</p>

<pre><code>{% set text = "A small boat surrounds a large ​island​​" %}

{% set linkedText = text|replace({'boat':'&lt;a href="/whatever"&gt;boat&lt;/a&gt;'}) %}

{{ linkedText|raw }}&lt;br&gt;&lt;br&gt;
</code></pre>

<p>That works.  </p>

<p>In theory there should be a way to do it with parseRefs too, but my initial test isn't working yet:</p>

<pre><code>{% set refText = text|replace({'boat':'&lt;a href="{entry:11102:}"&gt;boat&lt;/a&gt;'}) %}

{% set parsedText = refText|parseRefs %}

{{ parsedText|raw }}
</code></pre>

<p>...which leaves an unparsed ref tag so far.  I'm just heading out of the office but maybe a simple replace as above is enough to get you started?</p>

CC BY-SA 3.0
2016-08-10T07:26:46.247Z
2016-08-10T07:26:46.247Z
Jeremy Daalder
<p>Assuming you only need this to work on the front end of your site (i.e., you don't actually need to see the links inside the Redactor field in the control panel), something like this might do the trick:</p>

<pre><code>{# Get the rich text content #}
{% set text = entry.richTextField %}

{# Get all entries that could possibly be linked #}
{% set entries = craft.entries.section('someSection').limit(null) %}

{# Loop through entries, replacing any words matching entry titles with a link #}
{% for entry in entries #}
    {% set text = text|replace({(entry.title|lower):'&lt;a href="'~entry.url~'"&gt;'~entry.title|lower~'&lt;/a&gt;'}) %}
{% endfor %}

{# Render the text #}
{{ text|raw }}
</code></pre>

<p>Note that there are several possible issues with the above:</p>

<ul>
<li>You might see performance issues if you have a lot of entries to match (consider using the <a href="https://craftcms.com/docs/templating/cache" rel="nofollow"><code>{% cache %}</code></a> tag)</li>
<li>Lower/uppercase and capitalization issues are difficult to deal with (the above example only matches lowercase words and phrases to the entry titles)</li>
<li>Words and phrases with HTML tags inside won't match, i.e. "something <strong>something</strong>" wouldn't match the entry title "Something something"</li>
<li>Entries can have identical titles, so if you have more than one entry called "Something something" only the last one will be linked</li>
</ul>

CC BY-SA 3.0
2016-08-10T09:55:34.660Z
2016-08-10T09:55:34.660Z
Mats Mikkel Rummelhoff
<p>Has anyone implemented auto linking of words within a redactor field? For example, a rich text field would contain this:</p>

<blockquote>
  <p>​<em>A small <strong>boat</strong> surrounds a large ​<strong>island</strong>​</em>​</p>
</blockquote>

<p>In a channel there are entries titled <strong>boat</strong> and <strong>island</strong>. I would like to automatically link to these without the editor having to manually link them. </p>

<p>Not sure how approach this so any hints to get me on my way are most appreciated.</p>

CC BY-SA 3.0
2016-08-10T06:02:53.060Z
Are all the entries titled with a single word?
Mats Mikkel Rummelhoff
<redactor><search><richtext><replace>
Automatically link to entries in rich text field
# Automatically link to entries in rich text field

(Asked by: username_0 on 2016-08-10)

Has anyone implemented auto linking of words within a redactor field? For example, a rich text field would contain this:

> ​*A small **boat** surrounds a large ​**island**​*​

In a channel there are entries titled **boat** and **island**. I would like to automatically link to these without the editor having to manually link them.

Not sure how approach this so any hints to get me on my way are most appreciated.

**Tags:** redactor, search, richtext, replace
> **Comments:**
> **username_1**: Are all the entries titled with a single word?  

---

## 2 answers

### Answer by username_2 on 2016-08-10. Score: 3

How about just using the replace filter?
{% set text = "A small boat surrounds a large ​island​​" %}

{% set linkedText = text|replace({'boat':'boat'}) %}

{{ linkedText|raw }}

That works.  
In theory there should be a way to do it with parseRefs too, but my initial test isn't working yet:
{% set refText = text|replace({'boat':'boat'}) %}

{% set parsedText = refText|parseRefs %}

{{ parsedText|raw }}

...which leaves an unparsed ref tag so far.  I'm just heading out of the office but maybe a simple replace as above is enough to get you started?


---

### Answer by username_1 on 2016-08-10. Score: 1

Assuming you only need this to work on the front end of your site (i.e., you don't actually need to see the links inside the Redactor field in the control panel), something like this might do the trick:
{# Get the rich text content #}
{% set text = entry.richTextField %}

{# Get all entries that could possibly be linked #}
{% set entries = craft.entries.section('someSection').limit(null) %}

{# Loop through entries, replacing any words matching entry titles with a link #}
{% for entry in entries #}
    {% set text = text|replace({(entry.title|lower):''~entry.title|lower~''}) %}
{% endfor %}

{# Render the text #}
{{ text|raw }}

Note that there are several possible issues with the above:

You might see performance issues if you have a lot of entries to match (consider using the {% cache %} tag)
Lower/uppercase and capitalization issues are difficult to deal with (the above example only matches lowercase words and phrases to the entry titles)
Words and phrases with HTML tags inside won't match, i.e. "something something" wouldn't match the entry title "Something something"
Entries can have identical titles, so if you have more than one entry called "Something something" only the last one will be linked


---

