0) { $api_params = trim( get_site_option( 'iframely_api_params' ) ); if ( !empty( $api_params )) { $args['api_params'] = $api_params; } } $api_key = trim( get_site_option( 'iframely_api_key' ) ); if (!empty( $api_key )) { $args['api_key'] = $api_key; } if (is_iframely_amp( $args )) { $args['iframely'] = 'amp'; } return $args; } # Disable on RSS feeds as there's no JavaScript to run embed codes add_filter( 'the_content_feed', 'iframely_disable_on_feed', 1, 99 ); function iframely_disable_on_feed ( $content ) { add_filter( 'embed_defaults', 'iframely_add_feed_arg' ); wp_oembed_remove_provider( '#https?://[^\s]+#i' ); return $content; } function iframely_add_feed_arg ( $args) { $args['feed'] = 1; return $args; } # Make compatible with Automatic AMP-WP plugin: https://github.com/Automattic/amp-wp function is_iframely_amp ( $args ) { return (is_array($args) && array_key_exists('iframely', $args) && $args['iframely'] == 'amp') || (is_string($args) && strpos($args, 'iframely=amp') !== false) || (function_exists('is_amp_endpoint') && is_amp_endpoint()); } add_filter( 'oembed_fetch_url', 'maybe_add_iframe_amp', 10, 3 ); function maybe_add_iframe_amp( $provider, $args, $url ) { if (is_iframely_amp( $args ) && strpos($provider, '//iframe.ly') !== false) { $provider = add_query_arg( 'amp', '1', $provider ); } return $provider; } add_filter( 'embed_oembed_html', 'iframely_filter_oembed_result', 10, 3 ); function iframely_filter_oembed_result( $html, $url, $args ) { if (strpos($html, ' if ( strpos($content, '', $content ); $content = ''; foreach ( $chunks as $chunk ) { $start = strpos($chunk, ''; $p = wpautop(substr( $chunk, 0, $start)); $content .= $p . $iframe; } } else { $content = wpautop($content); } return $content; } add_filter( 'amp_content_embed_handlers', 'maybe_disable_default_embed_handlers', 10, 2 ); function maybe_disable_default_embed_handlers($embed_handler_classes) { return ! get_site_option( 'iframely_only_shortcode' ) ? array() : $embed_handler_classes; }; # Add URL options to Gutenberg # 'oembed_default_width' filter is used only in oembed/1.0/rest - i.e in the editor add_filter( 'oembed_default_width', 'iframely_flag_ajax_oembed' ); function iframely_flag_ajax_oembed( $width ) { add_filter( 'embed_defaults', 'iframely_bust_gutenberg_cache', 10, 1 ); add_filter( 'oembed_fetch_url', 'maybe_add_gutenberg_1', 10, 3 ); add_filter( 'oembed_result', 'inject_events_proxy_to_gutenberg', 10, 3 ); # The core's code doesn't even bother to look into default values and just hardcodes 600. # since we use the filter anyway - let's fix that if ( ! empty( $GLOBALS['content_width'] ) ) { $width = (int) $GLOBALS['content_width']; } return $width; } function iframely_bust_gutenberg_cache( $args) { $args['gutenberg'] = 1; return $args; } function maybe_add_gutenberg_1( $provider, $args, $url ) { if (strpos($provider, '//iframe.ly') !== false) { $provider = add_query_arg( 'iframe', '1', $provider ); } return $provider; } function inject_events_proxy_to_gutenberg( $html, $url, $args ) { if (isset($html) && $html != '' ) { // != trims $html return $html. ''; } else { return $html; } }; add_action( 'enqueue_block_editor_assets', 'iframely_gutenberg_loader' ); function iframely_gutenberg_loader() { $blockPath = "ui/iframely.js"; $ifcdn = 'https://if-cdn.com/'; // Load iframly CDN scripts wp_enqueue_script( 'iframely-embed', $ifcdn.'embed.js', array( 'jquery' ) ); wp_enqueue_script( 'iframely-options', $ifcdn.'options.js', array( 'jquery' ) ); // Register plugin Admin functionality wp_register_script( 'iframely', plugins_url($blockPath, __FILE__), array( 'wp-i18n', 'wp-element', 'wp-blocks', 'wp-components', 'wp-api', 'wp-editor' ), filemtime(plugin_dir_path(__FILE__) . $blockPath), true ); wp_enqueue_script('iframely'); } # fix cache ttl add_filter('oembed_ttl', 'maybe_disable_cache', 99, 4); function maybe_disable_cache($ttl, $url, $attr, $post_ID) { $iframely_ttl = DAY_IN_SECONDS * (int)get_site_option('iframely_cache_ttl'); if ($iframely_ttl > 0) { global $wp_embed; # Copy keys from wp-embed $key_suffix = md5( $url . serialize( $attr ) ); $cachekey = '_oembed_' . $key_suffix; $cachekey_time = '_oembed_time_' . $key_suffix; $cache_time = get_post_meta( $post_ID, $cachekey_time, true ); if ( ! $cache_time || (time() - $cache_time > $iframely_ttl) ) { $wp_embed->usecache = false; delete_post_meta( $post_ID, $cachekey_time); delete_post_meta( $post_ID, $cachekey); return $iframely_ttl; } else { return $ttl; } } else { return $ttl; } } # Register [iframely] shortcode add_shortcode( 'iframely', 'embed_iframely' ); # rewrite oembed discovery add_filter( 'oembed_endpoint_url', 'publish_embeds_via_iframely', 10, 2) ; function publish_embeds_via_iframely($url, $permalink, $format = 'json') { if ('' !== $permalink && get_site_option( 'publish_iframely_cards')) { $endpoint = iframely_create_api_link ('discovery'); $endpoint = add_query_arg( array( 'url' => urlencode( $permalink ), 'format' => strpos($url, 'format=xml') ? 'xml': 'json' // $format isn't passed inside the filter for some reason, hence the workaround ), $endpoint ); return $endpoint; } else { return $url; } } # Function to process content in iframely shortcode, ex: [iframely]http://anything[/iframely] function embed_iframely( $atts, $content = '' ) { # Read url from 'url' attribute if not empty if ( !empty( $atts['url'] ) ) $content = $atts['url']; $content = str_replace( '&', '&', trim( $content ) ); $content = str_replace( '&', '&', $content ); # Get global WP_Embed class, to use 'shortcode' method from it global $wp_embed; # With API key we can use iframely as provider for any url inside our shortcode # Add handler, if it wasn't added before $only_shortcode_before = get_site_option( 'iframely_only_shortcode' ); if ($only_shortcode_before) { iframely_update_option('iframely_only_shortcode', null); $atts['iframely_shortcode'] = true; } # Get embed code for the url using internal wp embed object (it caches results for the post automatically) $code = $wp_embed->shortcode( $atts, $content ); # return only_shortcode option to what it was before if ($only_shortcode_before) { iframely_update_option('iframely_only_shortcode', $only_shortcode_before); } # Return code to embed for the url inside shortcode return $code; } # Create link to iframely API backend function iframely_create_api_link ($origin = '') { # Read API key from plugin options $api_key = trim( get_site_option( 'iframely_api_key' ) ); $link = $api_key ? 'http://iframe.ly/api/oembed': 'http://open.iframe.ly/api/oembed'; $link = add_query_arg( array( 'origin' => '' !== $origin ? $origin : preg_replace( '#^https?://#i', '', get_bloginfo( 'url' ) ), 'api_key' => $api_key ? $api_key : false ), $link ); $api_params = trim( get_site_option( 'iframely_api_params' ) ); if ( !empty( $api_params )) { parse_str( $api_params, $params ); $link = add_query_arg( $params, $link ); } return $link; } # Create iframely settings menu for admin add_action( 'admin_menu', 'iframely_create_menu' ); add_action( 'network_admin_menu', 'iframely_network_admin_create_menu' ); # Create link to plugin options page from plugins list function iframely_plugin_add_settings_link( $links ) { $settings_link = 'Settings'; array_push( $links, $settings_link ); return $links; } $iframely_plugin_basename = plugin_basename( __FILE__ ); add_filter( 'plugin_action_links_' . $iframely_plugin_basename, 'iframely_plugin_add_settings_link' ); # Create new top level menu for sites function iframely_create_menu() { add_menu_page('Iframely Options', 'Iframely', 'install_plugins', 'iframely_settings_page', 'iframely_settings_page'); } # Create new top level menu for network admin function iframely_network_admin_create_menu() { add_menu_page('Iframely Options', 'Iframely', 'manage_options', 'iframely_settings_page', 'iframely_settings_page'); } function iframely_update_option($name, $value) { return is_multisite() ? update_site_option($name, $value) : update_option($name, $value); } function iframely_settings_page() { ?>

How to use Iframely

Iframely will take URL in your post, it to the cloud API parsers and replace it with the embed code we could find. Responsive, if possible. We cover well over 2000 domains. Plus summary cards for other URLs, including your own site. You can test some URLs here.

  • Put URL on a separate line - standard WP way

  • Or use shortcode [iframely]http://...[/iframely]

Note: Some people expect Iframely to wrap URLs with <iframe src=...> code. That's not what Iframely is for. Iframely needs a canonical URL, and we'll find the embed codes/iframes ourselves.

Configure your settings

These are the options of this plugin only. More Iframely features are available in your account's dashboard at iframely.com. API key and account with Iframely is only required if/when you go live. You can just skip API key field for testing purposes. Albeit, you won't see some features, say URL options in your Guttenberg editor and AMP embeds.

  • Your Iframely API Key (get one at iframely.com):

  • Optional API query-string params (see this doc):

  • Cache the embed codes for this number of days:

    By default, WordPress will refresh embed codes only when you edit and save a post. This isn't right. Embed codes should be refreshed periodically.
    Configure how often it will be done. As in "Once every XX days for each post". Enter 0 to skip Iframely's cache handler and use WP defaults.

  • checked="checked" /> Don't override default embed providers

    It will block Iframely from intercepting all URLs in your editor that may be covered by other embeds plugins you have installed, e.g. a Jetpack, or default embeds supported by WordPress (including AMP). Although, we should support default WP providers too, just make it better. Say, add URL options editor.

  • checked="checked" /> Use Iframely summary cards as embeds for your site

    Since WP 4.4 your site publishes embeds by default so that your own and other WP sites can embed summaries of your posts.
    Use this option to override the default widgets and use nice Iframely cards instead.
    Customize design of your cards here. Preview your Iframely cards here.