X

How to Embed iframe in WordPress without a Plugin

Snippets by IsItWP

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.

Comments   Leave a Reply

  1. Facciolini Rosetta November 13, 2015 at 2:12 pm

    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

  2. glad you like it!  shout out to @tweetsfromchris:twitter for this one, actually!

  3. Surkho Salamov July 11, 2011 at 1:24 pm

    Thanks! great and useful feature. 

    1. Cool glad you like the snippet. Shout out to Justin for this one!

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!