<p>Have you considered using the <a href="https://docs.craftcms.com/v3/matrix-fields.html" rel="nofollow noreferrer">Matrix</a> field type? You could create a block for rich text, then a block for a contact form (or whatever widget).</p>

<p>You could probably achieve what you're asking with a plugin but in my experience doing things like this in Redacter are prone to weirdness/bugginess (especially when in the hands of a client).</p>

CC BY-SA 4.0
2019-08-22T12:39:11.420Z
I would rather avoid that approach, as the content is logically a single unit: an article. At the moment, I use page breaks, which works fine for site-wide widgets (like ad units), but not so well for entry-specific widgets (like sponsors).
nicbou
CC BY-SA 4.0
2019-08-22T12:47:41.450Z
Perhaps I miss-understand but there needn't be any difference in the way it displays on the front end ie. the content can still look like:
<article>
    <p>You rich text output</p>
    <form>Your form</form>
    <p>More rich text</p>
</article>
dmatthams
CC BY-SA 4.0
2019-08-22T14:25:44.783Z
Cutting the content in pieces with a Matrix field seems like the wrong approach. The content (e.g. an article) is a single unit and should be a single unit in the database. The widget is more akin to a bit of template: it contains server-side logic like a CSRF tag. It's not a static element.
nicbou
CC BY-SA 4.0
2019-08-22T14:44:56.857Z
In that case, it's a case of extending redactor.
dmatthams
CC BY-SA 4.0
2019-08-22T16:09:11.067Z
@NicolasBouliane Cutting the article up into matrix blocks is an _excellent_ way to approach this problem. A matrix field is still a single field, I don’t understand what the hang up is.
Lindsey D
CC BY-SA 4.0
2019-08-22T16:11:06.587Z
This article describes the purpose and architecture very well... https://nystudio107.com/blog/creating-a-content-builder-in-craft-cms
Lindsey D
CC BY-SA 4.0
2019-08-23T05:45:05.857Z
Lindsey, you are right. I have simply misunderstood what a matrix field was. I've toyed around with it today and it's exactly what I need. Is there an easy way to migrate the content from a rich text field to a matrix field?
nicbou
CC BY-SA 4.0
2019-08-22T10:24:15.363Z
2019-08-22T10:24:15.363Z
dmatthams
<p>I want to insert widgets in arbitrary positions in one of my rich text fields. A good example would be a contact form inserted in the middle of the content. These widgets would be ad units, forms and similar dynamically rendered templates.</p>

<p>I know about the widget plugin for Redactor, but this is only suitable for static HTML. I need something a bit closer to an <code>{% include %}</code> tag.</p>

<p>How would you approach this problem?</p>

<p>I considered using WordPress-like <code>[insert-widget-here]</code> tags, or perhaps iFrames, but I'd prefer a more elegant solution.</p>

<templating><editor>
How to insert widgets in Rich Text field
# How to insert widgets in Rich Text field

(Asked by: username_0 on 2019-08-22)

I want to insert widgets in arbitrary positions in one of my rich text fields. A good example would be a contact form inserted in the middle of the content. These widgets would be ad units, forms and similar dynamically rendered templates.

I know about the widget plugin for Redactor, but this is only suitable for static HTML. I need something a bit closer to an `{% include %}` tag.

How would you approach this problem?

I considered using WordPress-like `[insert-widget-here]` tags, or perhaps iFrames, but I'd prefer a more elegant solution.

**Tags:** templating, editor

---

## 1 answers

### Answer by username_1 on 2019-08-22. Score: 3 | Accepted answer

Have you considered using the [Matrix](https://docs.craftcms.com/v3/matrix-fields.html) field type? You could create a block for rich text, then a block for a contact form (or whatever widget).

You could probably achieve what you're asking with a plugin but in my experience doing things like this in Redacter are prone to weirdness/bugginess (especially when in the hands of a client).

> **Comments:**
> **username_0**: I would rather avoid that approach, as the content is logically a single unit: an article. At the moment, I use page breaks, which works fine for site-wide widgets (like ad units), but not so well for entry-specific widgets (like sponsors).  
> **username_1**: Perhaps I miss-understand but there needn't be any difference in the way it displays on the front end ie. the content can still look like: <article>     <p>You rich text output</p>     <form>Your form</form>     <p>More rich text</p> </article>  
> **username_0**: Cutting the content in pieces with a Matrix field seems like the wrong approach. The content (e.g. an article) is a single unit and should be a single unit in the database. The widget is more akin to a bit of template: it contains server-side logic like a CSRF tag. It's not a static element.  
> **username_1**: In that case, it's a case of extending redactor.  
> **username_2**: @NicolasBouliane Cutting the article up into matrix blocks is an _excellent_ way to approach this problem. A matrix field is still a single field, I don’t understand what the hang up is.  
> **username_2**: This article describes the purpose and architecture very well... https://nystudio107.com/blog/creating-a-content-builder-in-craft-cms  
> **username_0**: Lindsey, you are right. I have simply misunderstood what a matrix field was. I've toyed around with it today and it's exactly what I need. Is there an easy way to migrate the content from a rich text field to a matrix field?  

---

