Add HTML to single post tag

If I have developed a separate HTML page that I wish to include inside my WordPress site what is the best way of including it?
In this case it requires a javascript library or stylesheet that is not by default in my WordPress theme that would need to be inserted in the tag.

I am aware of the following solutions:

iFrames:

  • To match the site styling I would need to include every css from the theme in the iframe header.
  • If the theme is updated with another script style sheet, this would not be updated in the iframe, maintenance could be arduous.

Editing the theme:

I could add into the various theme files along the lines:

is_single( 'page1' )
add_action( 'wp_head', 'extra_stuff' )

For each post/page I create or change I would have to change the theme files accordingly.

Ideally I would like to be able to do this from within the post editor

Are there any plugins that accomplish this?

2 Answers
2

I found the best solution was to add a custom field, head, and then build a short plugin to add the contents to the head tag:

add_action('wp_head', 'add_HTML_head');
function add_HTML_head(){
    global $post;
    if(!empty($post)){
        // get custom field for 'head'
        $headHTML = get_post_meta($post->ID, 'head', true);
        if(!empty($headHTML)){
            echo $headHTML;
        }
    }
}

I’ve packaged it up into a simple plugin.

Leave a Comment