I have in my CiviCRM a custom field which is a memo field with a text editor. If I update the contents of this field through the user interface contents is saved correctly as html. E.g. <p>Hello world</p>

However when I use the api (for example through the api explorer) the html gets malformed. Entering the same html results in the following: &gt;&lt;Hello world&gt;/p&lt;

How do I update the contents of a custom field which should html code?

Update: I disocvered there is an API Wrapper in place which encodes the html input. This wrapper gets executed for every api call. See https://github.com/civicrm/civicrm-core/blob/master/CRM/Utils/API/HTMLInputCoder.php#L147

Thanks, Jaap Jansma

  • Aren't custom fields in getSkipFields? What kind of API call are you using? Can you give an example?
    – bgm
    May 27, 2019 at 15:31

1 Answer 1


I created a contact custom field with data type "Note" and field type "RichTextEditor" and then ran this API call:

CRM.api3('Contact', 'create', {
  "contact_type": "Individual",
  "id": 180,
  "custom_15": "<p><strong>foobar</strong></p>"

The value was stored as-is, and shows correctly in the UI.

This part of CRM_Utils_API_HTMLInputCoder should ensure that custom fields of type RichTextEditor are excluded.

One scenario in which this might not work: Let's say you have another API wrapper that renames a parameter from, say, food_preference to custom_22. If the renaming API wrapper runs after CRM_Utils_API_HTMLInputCoder (not sure what the order is there, I'd guess core ones run first), food_preference would already be encoded before it gets to your wrapper.

  • Thanks pfigel! As I read it correctly your answer works from CiviCRM version 5.13.4 and newer. My question was related to CiviCRM 5.4. May 28, 2019 at 8:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.