Do you want to embed iframe on your WordPress site? While probably there’s a plugin to do this, you can add this simple code snippet to functions.php to embed iframe using a shortcode.
Instructions:
All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin:
add_shortcode('iframe', array('iframe_shortcode', 'shortcode')); class 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="" width="'.$width.'" height="'.$height.'" scrolling="'.$scrolling.'" frameborder="'.$frameborder.'" marginheight="'.$marginheight.'"><a href="'.$url.'" target="_blank">'.$url.'</a></iframe>'; } }
Here’s an example of how to embed the iframe using a shortcode.
[iframe url="http://wpsnipp.com" width="100" height="100" scrolling="yes" frameborder="1" marginheight="2"]
Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly copy / paste code snippets in WordPress, so you don’t accidentally break your site.
If you liked this code snippet, please consider checking out our other articles on the site like: 62 best free WordPress blog themes or 7 best WordPress contact form plugins.
Great! Thanks.
I just wish to become responsive height, or, if imposed, for example: ‘height’ => ‘300’, with a small monitor disappears a piece of content
glad you like it! shout out to @tweetsfromchris:twitter for this one, actually!
Thanks! great and useful feature.
Cool glad you like the snippet. Shout out to Justin for this one!