Make Iframes Multilingual

By default iframes are not multilingual because they are HTML code. but there’s a way to easily make them mutlilingual without too much hassle.

Please note: The code below works at the moment of the publishing of this snippet, however On The Go System is not responsible for its usages nor it will be supported, so please use with caution and take this snippet as a suggestion on what you could do.


function my_embed( $atts ) {
	$a = shortcode_atts( array(
		'src' => 'default_url_goes_here',
	), $atts );

	$awesome_iframe='<iframe width="100%" height="300" scrolling="no" frameborder="no" allow="autoplay" src="' . $a[src] . '"></iframe>';
	return $awesome_iframe;
}
add_shortcode( 'custom_iframe', 'my_embed' );

The code goes into your active theme’s function.php file and can be used by adding the “src” parameter in the shortcode from the “src” parameter in the iframe code you want to use.

Example

If the iframe looks like this:

<iframe width="100%" height="300" scrolling="no" frameborder="no" allow="autoplay" src="https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/playlists/1075752043&color=%23ff5500&auto_play=false&hide_related=false&show_comments=true&show_user=true&show_reposts=false&show_teaser=true&visual=true">

The shortcode will look like this:

[custom_iframe src="https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/playlists/1075752043&color=%23ff5500&auto_play=false&hide_related=false&show_comments=true&show_user=true&show_reposts=false&show_teaser=true&visual=true"]

Now all you have to do is register the shortcode to be translated with WPML using this guide: https://wpml.org/documentation/support/language-configuration-files/#page-builder-content

It would end up looking like this and you can add it in WPML > Settings > Custom XML Configuration:

<wpml-config>
   <shortcodes>
        <shortcode>
            <tag>custom_iframe</tag>
            <attributes>
                <attribute>src</attribute>
            </attributes>
        </shortcode>
   </shortcodes>
</wpml-config>

And that’s it! you can translate IFRAMES now.


Let us know if this snippet is not working for you:

This snippet doesn’t work
5 1 vote
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments