Search code examples
wordpressiframeshortcode

wordpress shortcode that embeds iframe executes only once per page


I have the following wordpress shortcode

function wiki_show( $atts  ) {

    // Attributes
    $atts = shortcode_atts(
        array(
            'name' => '',
            'height' => '500',
        ),
        $atts,
        'wiki'
    );

    // Return custom embed code
    return '<div><iframe src="https://en.wikipedia.org/wiki/' . '&name=' . $atts['name'] . '" height="' . $atts['height'] . '"/></div> ';

}
add_shortcode( 'wiki', 'wiki_show' );

I use it within a wordpress page like so:

AAAAAAAA

[wiki  name="Wolfenstein_3D" height="500"]

BBBBBBB

[wiki name="Nelson_Mandela" height="800"]

CCCCCCCC

The problem is that after the first iframe, no content is displayed (not even the BBBB.... that is after the first iframe)

If I change the shortcode code to tag some "foo" instead of "iframe" it shows all the code correctly.

If I embed the 2 iframes manually (without the shortcode) it works fine.

How do I make the shortcode work? What am I doing wrong?

(*credit: this code was adapted from: https://generatewp.com/shortcodes/?clone=video-embed-shortcode)


Solution

  • The <iframe> element isn't a self closing element. You should change it to

    return '<div><iframe […]></iframe></div>';
    

    Also check out HTML validators, it would have catched this issue. ;-)