Answers.Body: <p>I wrote a plugin called <a href="https://github.com/mmikkel/RetconHTML-Craft" rel="nofollow">Retcon HTML</a> which can wrap HTML content in a safe manner using <a href="http://php.net/manual/en/class.domdocument.php" rel="nofollow">DOMDocument</a>.</p>
  
  <p>Here's how you could wrap all iframes in a container with the classname <code>inline-media</code>: </p>
  
  <p><code>{{ entry.body | retconWrap('iframe', '.inline-media') }}</code></p>
  
  <p><a href="https://github.com/mmikkel/RetconHTML-Craft/wiki/Wrap" rel="nofollow">Click here to read the wiki on the <code>wrap</code> method</a>.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-09-23T14:00:18.947Z
Answers.LastActivityDate: 2015-09-23T14:00:18.947Z
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.Body: <p>I suggest a Twig filter so you can do something like this in the template:</p>
  
  <pre><code>{{ entry.body | wrapembeds }}
  </code></pre>
  
  <p>This is something to love about Craft - the ease of building Twig filters.</p>
  
  <p>In the plugins directory create a folder <code>videoembeds</code>.</p>
  
  <p>Create the main plugin file <code>WrapEmbedsPlugin.php</code>:</p>
  
  <pre><code>&lt;?php
  namespace Craft;
  
  class WrapEmbedsPlugin extends BasePlugin
  {
      public function getName()
      {
          return Craft::t('Embed Wrapper Twig Filter');
      }
  
      public function getVersion()
      {
          return '0.0';
      }
  
      public function getDeveloper()
      {
          return 'Your Name';
      }
  
      public function getDeveloperUrl()
      {
          return 'http://yoursite.com';
      }
  
      public function hookAddTwigExtension()
      {
          Craft::import('plugins.wrapembeds.WrapEmbedsTwigExtension');
          return new WrapEmbedsTwigExtension();
      }
  }
  </code></pre>
  
  <p>Create another file <code>WrapEmbedsTwigExtension.php</code>:</p>
  
  <pre><code>&lt;?php
  namespace Craft;
  
  class WrapEmbedsTwigExtension extends \Twig_Extension
  {
    protected $env;
  
    public function getName()
    {
      return 'WrapEmbeds Twig Extension';
    }
  
    public function getFilters()
    {
      return array('wrapembeds' =&gt; new \Twig_Filter_Method($this, 'wrapembeds'));
    }
  
    public function initRuntime(\Twig_Environment $env)
    {
      $this-&gt;env = $env;
    }
  
    public function wrapembeds($content)
    {
      $filtered = preg_replace_callback('/&lt;iframe.+iframe&gt;/', function($matches) {
        return '&lt;div class="inline-media"&gt;'. $matches[0] . '&lt;/div&gt;';
      }, $content);
  
      $charset = craft()-&gt;templates-&gt;getTwig()-&gt;getCharset();
      $filtered = new RichTextData($filtered, $charset);
  
      return $filtered;
    }
  
  }
  </code></pre>
  
  <p>Enable the plugin in the admin then add the filter to the template tag. Hopefully this is enough to get you started adding custom Twig filters. Simple once you know how and very handy!</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-24T20:26:27.757Z
Answers.LastActivityDate: 2014-07-24T20:26:27.757Z
Answers.UserDisplayName: Sam Hernandez
Body: <p>When using rich text fields, is there a way to automatically wrap embedded content (specifically, videos) in some kind of custom HTML?</p>
  
  <p>I'd like to, at the very least, wrap the embed in some element (a <code>&lt;div&gt;</code>, for instance) with some kind of class or ID, so that it'd be something like <code>&lt;div class="inline-media"&gt;EMBED HERE&lt;/div&gt;</code>.</p>
  
  <p>I've found my way to <a href="http://buildwithcraft.com/docs/rich-text-fields">the Craft docs for rich text fields</a>, and see that I may be able to add this functionality via <a href="http://imperavi.com/redactor/docs/settings/">Redactor</a>, but I'm not sure how to go about doing so.</p>
  
  <p>Any suggestions are appreciated.</p>
  
Tags: <richtext><redactor>
Title: Wrap embeds with HTML in rich text field
ThreadText: # Wrap embeds with HTML in rich text field
  
  (Asked by: username_0 on 2014-07-24)
  
  When using rich text fields, is there a way to automatically wrap embedded content (specifically, videos) in some kind of custom HTML?
  
  I'd like to, at the very least, wrap the embed in some element (a `<div>`, for instance) with some kind of class or ID, so that it'd be something like `<div class="inline-media">EMBED HERE</div>`.
  
  I've found my way to [the Craft docs for rich text fields](http://buildwithcraft.com/docs/rich-text-fields), and see that I may be able to add this functionality via [Redactor](http://imperavi.com/redactor/docs/settings/), but I'm not sure how to go about doing so.
  
  Any suggestions are appreciated.
  
  **Tags:** richtext, redactor
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2014-07-24. Score: 8 | Accepted answer
  
  I suggest a Twig filter so you can do something like this in the template:
  
  ```
  {{ entry.body | wrapembeds }}
  
  ```
  
  This is something to love about Craft - the ease of building Twig filters.
  
  In the plugins directory create a folder `videoembeds`.
  
  Create the main plugin file `WrapEmbedsPlugin.php`:
  
  ```
  <?php
  namespace Craft;
  
  class WrapEmbedsPlugin extends BasePlugin
  {
      public function getName()
      {
          return Craft::t('Embed Wrapper Twig Filter');
      }
  
      public function getVersion()
      {
          return '0.0';
      }
  
      public function getDeveloper()
      {
          return 'Your Name';
      }
  
      public function getDeveloperUrl()
      {
          return 'http://yoursite.com';
      }
  
      public function hookAddTwigExtension()
      {
          Craft::import('plugins.wrapembeds.WrapEmbedsTwigExtension');
          return new WrapEmbedsTwigExtension();
      }
  }
  
  ```
  
  Create another file `WrapEmbedsTwigExtension.php`:
  
  ```
  <?php
  namespace Craft;
  
  class WrapEmbedsTwigExtension extends \Twig_Extension
  {
    protected $env;
  
    public function getName()
    {
      return 'WrapEmbeds Twig Extension';
    }
  
    public function getFilters()
    {
      return array('wrapembeds' => new \Twig_Filter_Method($this, 'wrapembeds'));
    }
  
    public function initRuntime(\Twig_Environment $env)
    {
      $this->env = $env;
    }
  
    public function wrapembeds($content)
    {
      $filtered = preg_replace_callback('/<iframe.+iframe>/', function($matches) {
        return '<div class="inline-media">'. $matches[0] . '</div>';
      }, $content);
  
      $charset = craft()->templates->getTwig()->getCharset();
      $filtered = new RichTextData($filtered, $charset);
  
      return $filtered;
    }
  
  }
  
  ```
  
  Enable the plugin in the admin then add the filter to the template tag. Hopefully this is enough to get you started adding custom Twig filters. Simple once you know how and very handy!
  
  
  ---
  
  ### Answer by username_1 on 2015-09-23. Score: 1
  
  I wrote a plugin called [Retcon HTML](https://github.com/mmikkel/RetconHTML-Craft) which can wrap HTML content in a safe manner using [DOMDocument](http://php.net/manual/en/class.domdocument.php).
  
  Here's how you could wrap all iframes in a container with the classname `inline-media`:
  
  `{{ entry.body | retconWrap('iframe', '.inline-media') }}`
  
  [Click here to read the wiki on the `wrap` method](https://github.com/mmikkel/RetconHTML-Craft/wiki/Wrap).
  
  
  ---
  
  