How can a field be rendered as a  element? The problem:

I would like to be able to manage custom &lt;meta&gt; elements, on a per-item basis in a custom theme. An example would be for adding open graph and schema tags, but I'm really looking for a generic solution.

I'm aware of custom modules, such as Meta Tags, but I'd rather avoid relying on other modules at this point.

What I've Tried:

After looking at them_field it looked as though I should create a custom theme function for my custom field (field_test being used for this example). The theme function could add a &lt;meta&gt; tag and not return any content to be rendered.

In template.php:

&lt;?php
//I *did* actually use my theme name instead of THEMENAME
function THEMENAME_field__field_test($vars) {
    drupal_add_html_head(array(
        '#tag' =&gt; 'meta',
        '#attributes' =&gt; array(
            'content' =&gt; drupal_render($vars['items'][0]),
            'name' =&gt; 'test'
        )
    ), 'test');

    //I figured this would prevent the field from being rendered inline
    return '';
}


Using that code the custom &lt;meta&gt; element was correctly added to the &lt;head&gt;. Nevertheless, the field was still rendered on the page.

If I changed the output to:

return ' ';


The content was no longer rendered to the page, but an unnecessary space character would be output where the field was to be rendered.

Is there a better way to render a field as a &lt;meta&gt; element?
-or-
How can I prevent this themed field from rendering inline?
try to return drupal_add_html_head array directly

&lt;?php

function THEMENAME_field__field_test($vars) {
    return drupal_add_html_head(array(
        '#tag' =&gt; 'meta',
        '#attributes' =&gt; array(
            'content' =&gt; drupal_render($vars['items'][0]),
            'name' =&gt; 'test'
        )
    ), 'test');
 }