Inventory_Presser_Shortcode_Iframe
On This Page
Description Description
Creates a shortcode that makes it easy to create iframes. We've found this useful on most sites for a financing application that is hosted on a separate domain.
Source Source
File: includes/shortcode/class-shortcode-iframe.php
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | class Inventory_Presser_Shortcode_Iframe { /** * Adds two shortcodes * * @return void */ public function add() { add_shortcode( 'invp-iframe' , array ( $this , 'content' ) ); add_shortcode( 'invp_iframe' , array ( $this , 'content' ) ); } /** * Adds hooks that power the shortcode * * @return void */ public function add_hooks() { add_action( 'init' , array ( $this , 'add' ) ); } /** * Creates the HTML content of the shortcode * * @param array $atts An array of shortcode attributes. * @return string HTML that renders an iframe that expands to the height of its content */ public function content( $atts ) { $script_handle = 'invp-iframe-resizer' ; if ( ! wp_script_is( $script_handle , 'registered' ) ) { Inventory_Presser_Plugin::include_scripts_and_styles(); } wp_enqueue_script( $script_handle ); wp_add_inline_script( $script_handle , 'iFrameResize({ log:false,sizeWidth:true });' ); $atts = shortcode_atts( array ( 'width' => '100%' , 'height' => '5000' , 'scrolling' => 'yes' , 'src' => '' , 'class' => 'iframe-class' , 'frameborder' => '0' , 'title' => '' , ), $atts ); // Stock number may arrive in a querystring variable with key 'stock'. if ( isset( $_GET [ 'stock' ] ) ) { $atts [ 'src' ] = esc_url( add_query_arg( 'stock' , sanitize_text_field( wp_unslash( $_GET [ 'stock' ] ) ), $atts [ 'src' ] ) ); } $html = '<iframe' ; foreach ( $atts as $attr => $value ) { // ignore some attributes. $ignored_atts = array ( 'onclick' , 'onload' , 'onpageshow' , 'same_height_as' , ); if ( in_array( strtolower ( $attr ), $ignored_atts , true ) ) { continue ; } if ( '' !== $value ) { // adding all attributes. $html .= ' ' . esc_attr( $attr ) . '="' . esc_attr( $value ) . '"' ; } else { // adding empty attributes. $html .= ' ' . esc_attr( $attr ); } } $html .= '></iframe>' ; if ( isset( $atts [ 'same_height_as' ] ) ) { $html .= sprintf( '<script> document.addEventListener( "DOMContentLoaded" , function (){ var target_element, iframe_element; iframe_element = document.querySelector( "iframe.%s" ); target_element = document.querySelector( "%s" ); iframe_element.style.height = target_element.offsetHeight + "px" ; }); </script>', esc_attr( $atts [ 'class' ] ), esc_attr( $atts [ 'same_height_as' ] ) ); } return $html ; } } |
Expand full source codeCollapse full source codeView on Github