When you want simplify publishing of specific elements or code chunks, WordPress Shortcodes might come in handy. Basically it replaces a piece of text that you insert via the editor with something else, defined in your template or in a plugin.
Here’s an example on how to create a plugin that allows you to insert iframes using shortcodes. Regular iframe tags can be inserted via the HTML editor in WordPress, but the example below will give you a hint on what it can be used for.
<?php
/*
Plugin Name: Iframe Shortcode Plugin
Plugin URI: http://www.guidecloud.com/2010/07/14/writing-your-own-iframe-shortcode-plugin-for-wordpress/
Description: Add iframes to Posts and Pages within WordPress.
Author: GuideCloud.com
Version: 1.0
Author URI: http://www.guidecloud.com/
License: GPL2
*/
class guidecloud_iframe_shortcode {
function shortcode($atts, $content=null) {
extract(shortcode_atts(array(
'url' => '',
'scrolling' => 'no',
'width' => '100%',
'height' => '500',
'frameborder' => '0',
'marginheight' => '0',
), $atts));
if (empty($url)) return '<!-- Iframe: You did not enter a valid URL -->';
return '<iframe src="'.$url.'" title="" scrolling="'.$scrolling.'" width="'.$width.'" height="'.$height.'" frameborder="'.$frameborder.'" marginheight="'.$marginheight.'"><a href="'.$url.'" target="_blank">'.$url.'</a></iframe>';
}
}
add_shortcode('iframe', array('guidecloud_iframe_shortcode', 'shortcode'));
?>
Create a new file called iframe-shortcode.php, paste the code above and upload it to your wp-content/plugins/ directory. Go to “Plugins” in your WordPress administration and activate the plugin.
Once the plugin has been activated you can place iframe shortcodes, like the example below, into your WordPress posts.
[iframe url="http://www.linktoiframe.com/iframepage/" height="500" width="100%" scrolling="no" frameborder="0" marginheight="0"]
You can also download the plugin here:
WordPress Iframe Shortcode Plugin
More reading: