Home / admin / iframe shortcode for posts and pages
  • How to iframe shortcode for posts and pages


    Adding this snippet to the functions.php of your wordpress theme will add a new shortcode so that you can add an iframe in posts or pages to any URL.

    ( click code to copy )

    wordpress snippet : PHP

    add_shortcode('iframe', array('iframe_shortcode', 'shortcode'));
    class iframe_shortcode {
        function shortcode($atts, $content=null) {
                   '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>';
    ( click code to copy )

    wordpress snippet : PHP

    [iframe url="http://wpsnipp.com" width="100" height="100" scrolling="yes" frameborder="1" marginheight="2"]

    ( WordPress codex functions, hooks, in this snippet. )

    add_shortcode, shortcode_atts, wp,
  • Thanks! great and useful feature. 

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

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

  • 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