We were in need of a WordPress plugin allowing us to add any HTML code in our posts for our Filmmaking blog World Wide Angle. Looking around on Google a little I stumbled upon EmbedIt by Matteo Ionescu. The plugin allows you to add a tag in your code like [HTMLx] and have it replace by anything you enter as a meta info on your post with that same key HTMLx (where x is a number). (If this last line of explanation proved difficult to comprehend, you can read Matteo’s how to on his plugin page in details).

The plugin worked great and met the demand perfectly. I was curious to see if the HTML would also show in the RSS feed for the posts so I checked out the code to see how the whole thing worked. This is by reading the code that I realized that we could only place a maximum of 9 segments of HTML in any given post. I thought we were unlikely to reach this limit and moved on to something else.

Eventually, we did come to a situation when we wanted to write a post containing more than 9 segments of HTML. I therefore decided to improve the plugin myself. Starting from Matteo’s code I done a quick search on the WordPress method he used: get_post_meta. That lead me to a WordPress Codex page about Custom Fields where I found out about the function get_post_custom_keys. Putting all these methods together and adding a little brain grease I came up with the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
add_filter('the_content', 'substitute');
function substitute($content) {
  global $post;
  // get the html of the whole current post/page
  $out=$content;
  $metadata = get_post_custom_keys();
  if (is_array($metadata) && count($metadata) >0) {
    foreach ($metadata as $key) {
      $out.substr($key, 0, 4);
      if (substr($key, 0, 4) == "HTML") {
        $html = get_post_meta($post->ID, $key, true).'';
        $out=str_replace('['.$key.']',$html, $out);
      }
    }
  }
  return $out;
}

This new version of the plugin will search the custom fields for fields named HTMLxx and, when found, replace [HTMLxx] in the content by the value of the custom field. No more 1 to 9 restriction 😉

That done, I will submit the improved code to the plugin author and return to the redaction of my original post on World Wide Angle.

Filed under: Computing, PHP

Tagged with: , ,

Liked this page?

Tweet it

Subscribe to the RSS feed or sign up for the newsletter now.

Posts related to Add any HTML to your WordPress posts with EmbedIt (plugin – tweaked)

9 Comments to "Add any HTML to your WordPress posts with EmbedIt (plugin – tweaked)"

  1. Trackback on Samantha Halfon on August 21, 2009 at 9:40 pm

    Liked the WordPress EmbedIt plugin to add any HTML to a post. Was limited though, so I tweaked it http://bit.ly/L6Hqf @bduperrin #wordpress

  2. Trackback on samantha on August 21, 2009 at 11:40 pm

    Liked the WordPress EmbedIt plugin to add any HTML to a post. Was limited though, so I tweaked it http://bit.ly/L6Hqf @bduperrin #wordpress

  3. Trackback on samantha on August 22, 2009 at 7:08 am

    Add any HTML to your WordPress posts with EmbedIt (plugin – tweaked) http://bit.ly/QGkhR

  4. Comment by Gene on November 14, 2009 at 4:55 pm Reply

    I would like to use your changes. How do I add it? Does the code above replace the whole plugin or have to be added to it in some way?

  5. Comment by Gene on November 14, 2009 at 5:12 pm Reply

    when I tried replacing the whole plugin code I got a parce error on line 9. I am not a programmer so I dont know what that means. If you would, please send me an email on how to use your code.
    thanks

  6. Comment by shalfon on November 21, 2009 at 9:35 am Reply

    Hi, the code available in the post above should replace the whole original plugin code. I have uploaded a version you can download and place in your plugin folder. Hope it will fix your problem. Please go to http://www.samanthahalfon.net/resources/embed-it_modified.php.zip to download the file. Good luck

  7. Comment by denise on December 15, 2009 at 8:24 am Reply

    hi can you help me in my site here? my code in the post like this never showed up in IE :

    1
    <object data="http://www.azsnagger.com/AmazonGrabber.php" type="text/html" width="790px" height="700px"
  8. Comment by Pratish on February 6, 2010 at 1:44 pm Reply

    Hi.. is there any way of automatically generating the meta data (I have a cartoon website and I would like to create a ‘copy this toon to your website’ snippet and include it in every post)

    Sorry bit of a newbie at this and any help would be appreciated!

    Thanks
    Pratish

  9. Comment by shalfon on February 7, 2010 at 4:22 pm Reply

    Hi,

    To do what you describe, I would rather look at a plugin like Post Footer: http://wordpress.org/extend/plugins/add-post-footer/

    Hope this helps

Name (required)

Email (required)

Website

Leave a Reply

  • About Me


    photo of Samantha Halfon Samantha Halfon
    Software Engineer
    blueKiwi software
    Paris, France
    I enjoy playing with my computer(s), listening to Bob Dylan (and related artists) and watching movies (especially if they were directed by Martin Scorsese or John Cassavetes). Sometimes, I play a little guitar... If not doing any of the above, I am either riding a small red bike around Paris, or, making videos. About my videomaking please check out World Wide Angle and its blog.