Aug
21
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.
Tagged with: wordpress, World, world wide angle
Liked this page?
Subscribe to the RSS feed or sign up for the newsletter now.
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
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
Add any HTML to your WordPress posts with EmbedIt (plugin – tweaked) http://bit.ly/QGkhR
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?
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
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
hi can you help me in my site here? my code in the post like this never showed up in IE :
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
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