VividVisions • information design

All posts filed under “WordPress”

WordPress: Add iframes to your post using custom fields and shortcodes

The visual editor of WordPress (TinyMCE) loooves to remove iframes from the content. This can be extremely annoying when you want to add Google Maps or other iframe-based widgets to your post. There are several approaches to a solution:

  • Turn off the visual editor
    No problem for someone who has a good grasp of HTML. Not quite a solution, more of a workaround.
  • Edit the TinyMCE configuration files
    The required changes are simple but could be lost with the next update of the editor.
  • Use plugins
    I’m sure, there are tons of WordPress plugins which address exactly this issue but more plugins also mean more maintenance. Especially, when there is a much simpler solution.

Add the following code to the functions.php file of your theme:

function field_func($atts) {
   global $post;
   $name = $atts['name'];
   if (empty($name)) return;

   return get_post_meta($post->ID, $name, true);
}

add_shortcode('field', 'field_func');

Now you can display the value of a custom field in your post by adding the shortcode [field name=name-of-your-custom-field] to your content at the position where you would like it to be shown. Just make sure that the shortcode stands alone on a line, otherwise WordPress would wrap it in a paragraph, which would result in invalid HTML.

To add an iframe, simply paste the HTML code into the ”value” area of your custom field, like shown here:

WordPress field function

Voilá. Your iframe (or any other HTML code for that matter) will not be removed from your post and you can edit the code without having to switch to the HTML mode.

Update: You’ll need at least WordPress 2.5 to be able to use shortcodes.


TagThePress – das tagthe.net Plugin für WordPress 2.5

English: The description of TagThePress moved here.

Deutsch: Die Beschreibung von TagThePress befindet sich jetzt hier.


Was lange währt, wird endlich gut!

Endlich ist sie online, die neue Website! Ich möchte sie als Plattform nutzen, um über die Agentur, Web-Design, Web-Entwicklung und die Branche im Allgemeinen zu schreiben.

Umgesetzt wurde sie mit WordPress, das sich als zuverlässiges, robustes und rasch adaptierbares Blogging-Tool erwiesen hat. Continue reading…


Twitter

Loading my tweets...