En este tutorial aprenderás a insertar contenido externo mediante un iframe utilizando un shortcode personalizado en WordPress. Esto es útil para incrustar servicios como mapas, calendarios, formularios externos, libros, PDFs o cualquier página externa.
Solo tienes que añadir el siguiente código al archivo functions.php de tu tema activo (o tema hijo), o bien en un plugin personalizado.
add_shortcode( 'iframe', 'iframe_shortcode' );
function iframe_shortcode( $atts, $content = null ) {
$atts = shortcode_atts( array(
'url' => '',
'scrolling' => 'no',
'width' => '100%',
'height' => '500',
'frameborder' => '0',
'marginheight' => '0',
), $atts );
if ( empty( $atts['url'] ) ) return '';
return '<iframe src="' . esc_url( $atts['url'] ) . '" width="' . esc_attr( $atts['width'] ) . '" height="' . esc_attr( $atts['height'] ) . '" scrolling="' . esc_attr( $atts['scrolling'] ) . '" frameborder="' . esc_attr( $atts['frameborder'] ) . '" marginheight="' . esc_attr( $atts['marginheight'] ) . '">
<a href="' . esc_url( $atts['url'] ) . '" target="_blank">' . esc_html( $atts['url'] ) . '</a></iframe>';
}Cómo usar el shortcode:
Puedes insertar el iframe en cualquier entrada o página usando:
[iframe url="https://ejemplo.com" width="100%" height="400" scrolling="yes" frameborder="1" marginheight="2"]