I am using node_save() to alter a node field, but HTML code is showing escaped This is the code I execute with "Execute PHP" with the Devel module.

$lang = LANGUAGE_NONE; // Replace with ISO639-2 code if localizing
$node_type = 'my_writings'; // Machine name of the content type

$query = new EntityFieldQuery;
$result = $query
  -&gt;entityCondition('entity_type', 'node')
  -&gt;propertyCondition('type', $node_type)
  -&gt;execute();

if (!empty($result['node'])) {
  $nodes = entity_load('node', array_keys($result['node']));

  foreach($nodes as $node) {

    $str=&lt;&lt;&lt;END
 &lt;h3&gt; TITLE&lt;/h3&gt;
&lt;p&gt;
Hello world.
&lt;/p&gt; 
END;
    // Replace field_foo with the machine name of the field to update.
    // - 0 refers to specific within the field array, for when the field contains
    //    multiple values. If the field only has one value, it should be 0.
    $node-&gt;field_author_box[$lang][0]['value'] = $str;
    node_save($node);
  }
}


The output I get is this HTML code like this.

&lt;h3&gt; TITLE&lt;/h3&gt;
 &lt;p&gt;
    Hello world.
&lt;/p&gt; 


The input format is set to Full HTML, but the HTML tags are shown escaped, even if in the database they appear normal.



I tried to re-save the node; then, it is normal. I tried also to use html_entity_decode() without luck, if I do the same with body field then it works fine, but with a new field its showing escaped characters.

What could the problem be?
Drupal core does not modify the data you save in the database [*]. All filters are applied only when displaying the data - this ensures that you never loose any data.

Assuming that you are using Drupal 7, and that the field in question has filtered text enabled, then what is happening is that when you save your new field, you are not setting what the filter format should be - so it defaults to plain text. You need to set the format of the field when saving it. In your code, you would do this as follows:

$node-&gt;field_author_box[$lang][0]['value'] = $str;
$node-&gt;field_author_box[$lang][0]['format'] = 'full_html';
node_save($node);


[*] Though some contrib modules may do this. For instance WYSIWYG editors such as CKEditor do modify your data (though in that case only when using the UI).