Answers.Body: <p>As best as I can tell, there's no way to do this through the config file. Or rather, if there is, it wouldn't be pleasant to maintain and debug.</p>
  <p>However, Craft does provide an event trigger that gives you the opportunity to make changes to the HTMLPurifier Config when it's created.</p>
  <p>The easiest way to take advantage of this is by watching for this event from within a custom module.</p>
  <p>If you don't already have a custom module for your site, you can read Andrew's Welch's blog for detailed instructions on how / why you should have one: <a href="https://nystudio107.com/blog/enhancing-a-craft-cms-3-website-with-a-custom-module" rel="nofollow noreferrer">https://nystudio107.com/blog/enhancing-a-craft-cms-3-website-with-a-custom-module</a></p>
  <p>Once you have a custom module (or plugin), this is an example of the code you'll need to add to it to define your own rules:</p>
  <pre><code>// ...
  
  use craft\redactor\Field AS RedactorField;
  
  // ...
  
  class YourCustomModule extends Module
  {
      // ...
  
      public function init()
      {
          parent::init();
  
          // ...
  
          // CUSTOM HTMLPURIFIER RULES
          Event::on(
              RedactorField::class,
              RedactorField::EVENT_MODIFY_PURIFIER_CONFIG,
              function (Event $event) {
                  if( $event-&gt;config ) {
                      if( $def = $event-&gt;config-&gt;getDefinition('HTML', true) ) {
                          // http://htmlpurifier.org/docs/enduser-customize.html#addAttribute
                          $def-&gt;addAttribute('a', 'aria-whatever', 'Text');
                          $def-&gt;addAttribute('a', 'data-whatever', 'Text');
                      }
                  }
              }
          );
      }
  
      // ...
  }
  </code></pre>
  <p>The above example allows the attributes <code>aria-whatever</code> and <code>data-whatever</code> on <code>&lt;a&gt;</code> elements.</p>
  <p>Unfortunately, there's no way to allow wildcards (at least not that I can tell), so something like this won't work:</p>
  <p><code>$def-&gt;addAttribute('a', 'data-*', 'Text');</code></p>
  <p>Hope that helps.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-02-22T12:05:46.243Z
Answers.Text: There is extra config for htmlpurifier available at `/config/htmlpurifier/Default.json`. But for some reason adding `HTML.AllowedAttributes` and specifying data attributes there throws an error (Attribute "..." in element "..." not supported). So your solution is the only way for me. Thanks.
Answers.UserDisplayName: muffir
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-12-27T06:11:12.727Z
Answers.LastActivityDate: 2021-12-27T06:11:12.727Z
Answers.UserDisplayName: stevecomrie
Body: <p>I'm a little bit over my head trying to configure HTML purifier for my needs.</p>
  <p>I asked <a href="https://stackoverflow.com/questions/68298431/how-to-allow-curly-braces-in-href-attributes-inside-content-that-is-processed-wi">this question</a> in stackoverflow and it seems that I might need to do something like this:</p>
  <pre><code>$config = HTMLPurifier_Config::createDefault();
  $config-&gt;set('HTML.DefinitionID', 'trusted');
  
  if ($def = $config-&gt;maybeGetRawHTMLDefinition())
  {
      $def-&gt;addAttribute('a', 'href', 'Text');
  }
  
  $purifier = new HTMLPurifier($config);
  </code></pre>
  <p>From what I gather, this means that I need to &quot;add an attribute to the HTML Purifier definition&quot;.</p>
  <p>So, my question is: <strong>Is there a way to do this with the .json purifier config file? Or should I try a custom plugin as described <a href="https://craftcms.stackexchange.com/questions/16366/how-to-customize-html-purifier">here</a>?</strong></p>
  <p>I've never built a plugin, so obviously I would prefer the former. In any case, I would appreciate any pointers! Thanks in advance!</p>
  
Tags: <redactor>
Title: How to add an attribute to the HTML Purifier definition
ThreadText: # How to add an attribute to the HTML Purifier definition
  
  (Asked by: username_0 on 2021-07-10)
  
  I'm a little bit over my head trying to configure HTML purifier for my needs.
  
  I asked [this question](https://stackoverflow.com/questions/68298431/how-to-allow-curly-braces-in-href-attributes-inside-content-that-is-processed-wi) in stackoverflow and it seems that I might need to do something like this:
  
  ```
  $config = HTMLPurifier_Config::createDefault();
  $config->set('HTML.DefinitionID', 'trusted');
  
  if ($def = $config->maybeGetRawHTMLDefinition())
  {
      $def->addAttribute('a', 'href', 'Text');
  }
  
  $purifier = new HTMLPurifier($config);
  
  ```
  
  From what I gather, this means that I need to "add an attribute to the HTML Purifier definition".
  
  So, my question is: **Is there a way to do this with the .json purifier config file? Or should I try a custom plugin as described [here](https://craftcms.stackexchange.com/questions/16366/how-to-customize-html-purifier)?**
  
  I've never built a plugin, so obviously I would prefer the former. In any case, I would appreciate any pointers! Thanks in advance!
  
  **Tags:** redactor
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-12-27. Score: 2
  
  As best as I can tell, there's no way to do this through the config file. Or rather, if there is, it wouldn't be pleasant to maintain and debug.
  
  However, Craft does provide an event trigger that gives you the opportunity to make changes to the HTMLPurifier Config when it's created.
  
  The easiest way to take advantage of this is by watching for this event from within a custom module.
  
  If you don't already have a custom module for your site, you can read Andrew's Welch's blog for detailed instructions on how / why you should have one: <https://nystudio107.com/blog/enhancing-a-craft-cms-3-website-with-a-custom-module>
  
  Once you have a custom module (or plugin), this is an example of the code you'll need to add to it to define your own rules:
  
  ```
  // ...
  
  use craft\redactor\Field AS RedactorField;
  
  // ...
  
  class YourCustomModule extends Module
  {
      // ...
  
      public function init()
      {
          parent::init();
  
          // ...
  
          // CUSTOM HTMLPURIFIER RULES
          Event::on(
              RedactorField::class,
              RedactorField::EVENT_MODIFY_PURIFIER_CONFIG,
              function (Event $event) {
                  if( $event->config ) {
                      if( $def = $event->config->getDefinition('HTML', true) ) {
                          // http://htmlpurifier.org/docs/enduser-customize.html#addAttribute
                          $def->addAttribute('a', 'aria-whatever', 'Text');
                          $def->addAttribute('a', 'data-whatever', 'Text');
                      }
                  }
              }
          );
      }
  
      // ...
  }
  
  ```
  
  The above example allows the attributes `aria-whatever` and `data-whatever` on `<a>` elements.
  
  Unfortunately, there's no way to allow wildcards (at least not that I can tell), so something like this won't work:
  
  `$def->addAttribute('a', 'data-*', 'Text');`
  
  Hope that helps.
  
  > **Comments:**
  > **username_2**: There is extra config for htmlpurifier available at `/config/htmlpurifier/Default.json`. But for some reason adding `HTML.AllowedAttributes` and specifying data attributes there throws an error (Attribute "..." in element "..." not supported). So your solution is the only way for me. Thanks.  
  
  ---
  
  