Printing fields in theme: Undefined index: safe_value I want to print my fields in tpl.php files as cleanly as possible. To do this, I have used the following code in my template.php file:

function print_field($field_name, $format = 'safe_value') {

if (arg(0) == 'node' &amp;&amp; is_numeric(arg(1))) {
  // Get the nid
  $nid = arg(1);

  // Load the node if you need to
  $node = node_load($nid);
}

$field= field_get_items('node', $node, $field_name);

if ($format == 'url') {
    print file_create_url($field[0]['uri']);
} else {
print $field[0]["$format"];
}
} 


Now, I can print my fields in my tpl.php file in a clean manner, without any unnecessary mark-up or labels:

&lt;?php print_field('field_section') ?&gt; -- Prints the field with safe formating

&lt;?php print_field('field_title', 'value') ?&gt;  -- Prints the field in Raw

&lt;?php print_field('field_intro_image', 'url') ?&gt; -- Prints the image url of a field

It all works fine, but I get this error message:


Notice: Undefined index: safe_value in print_field() (line 19 of template.php).



How I can resolve this? 

Also, is it possible to just enter value or url in my tpl.php file without wrapping them in quote marks?
The problem is with your function, not the template. 

Ideally you should check if the index exists, e.g.

print array_key_exists($format, $field[0]) ? $field[0]["$format"] : '';


or:

print !empty($field[0]["$format"]) ? $field[0]["$format"] : '';


or:

print @$field[0]["$format"] ? $field[0]["$format"] : '';


Alternatively you can ignore the notices in PHP config (php.ini) or in Drupal admin.

Or you can just prefix the field with @ to silence/suppress any errors (usually not recommended), e.g.

print @$field[0]["$format"];