iframes in Wordpress - wordpress

I am trying to generate some embeddable iframe code so a user can include the code on a blog to display content I'm planning to deliver. Right now the content I'm trying to deliver is just a website. I am trying to use the following code on a Wordpress blog:
<iframe width="420" height="315" src="http://www.cnn.com" frameborder="0"></iframe>
However when the page is viewed Wordpress simply outputs a link for "http://www.cnn.com" based on the following html.
http://www.cnn.com
That said, if I use Youtube generated iframe code, the iframe loads fine. For example:
<iframe width="420" height="315" src="http://www.youtube.com/embed/_OBlgSz8sSM" frameborder="0" allowfullscreen></iframe>
results in:
<iframe class='youtube-player' type='text/html' width='420' height='315' src='http://www.youtube.com/embed/_OBlgSz8sSM?version=3&rel=1&fs=1&showsearch=0&showinfo=1&iv_load_policy=1&wmode=transparent' frameborder='0'></iframe>
Any idea what Youtube is doing to enable this functionality or more generally how to get my simply iframe to work.

I don't know why you get an anchor instead of the iframe, but I know that google don't want their homepage in iframes. If you would have an iframe with the src http://www.google.com, you would see an empty iframe. Also see this example.
=== UPDATE ===
Wordpress prohibits iframes with few exceptions. Probably you can handle it with shortcodes. Try adding following untested code into the functions.php in your theme.
// [iframe src="www.cnn.com"]
function iframe_func($atts) {
extract(shortcode_atts(array(
'src' => 'default'
), $atts));
return '<iframe src="{$src}"></iframe>';
}
add_shortcode('iframe', 'iframe_func');
Now you can add [iframe src="www.cnn.com"] in the article editor in the wordpress admin.

Creating a shortcode is the way I get around this problem. It bypasses the WYSIWYG editor and puts the html in the page.
I would approach it like this.
Add this to your functions.php file:
function add_iframe($atts) {
extract(shortcode_atts(array(
'src' => '/'
), $atts));
$theframe = '<iframe src="'.$src.'" width="420" height="315" frameborder="0"></iframe>';
return $theframe;
}
add_shortcode('iframe', 'add_iframe');
Useage:
Add [iframe src=http://thesiteyouwanttoshow.com] to the content where you want the iframe to show.

If you are loading your own web page within the iframe remember that most hosted solutions will have xFrame options set to SAMEORIGIN, so no matter what you change in Wordpress, the page will still not render as it is being blocked by the target website.
I spent hours with this problem so hopefully if you are having this issue you will the target website as well. If you are hosting a solution on rails the answer I found is here and a website that will definitely load in Wordpress can be found here, so feel free to use that endpoint as a test.

Google uses the X-Frame-Options header (set to SAMEORIGIN) to prevent you from placing it in an iframe. Getting around this would require the user to use a browser that doesn't support X-Frame-Options.

Unfortunately the major search engine sites such as google and yahoo (bing excluded) don't allow for iframe embedding since they offer a plethora of APIs and integration options. So there is really no real way for you to do this. If you are not planning on embedding google as your iframe source then you should be in the clear with the current code that you have in place. Try it out and just change the source to something else - google will not show up in its place. If you want a search engine there unfortunately it is with horror that I say that bing is the only one that works.
Hope this helps!
So in recap - Google does not embed in iframe, but other content that you produce should based on your coding:
example:
<iframe width="420" height="315" src="http://www.uncrate.com" frameborder="0"></iframe>
http://jsfiddle.net/pKby8/
The Anchor that you get is a result of the xFrame option. When it connects to the google servers the servers kick back a cute response hinting that you should link to them instead of iframe.

I would create a WordPress short_code that would insert your iFrame in the output. I think by adding the iFrame code directly in the post box wordpress is changing it.
http://codex.wordpress.org/Shortcode_API

Related

Dynamically pass parameter to Elementor's HTML Code block

I have a page created with Elementor. Nothing fancy. Just a Section, inside of which there is a Column, inside of which there is an HTML Code block. Inside the HTML block there is just an iFrame, like <iframe src="https://app.squarespacescheduling.com/schedule.php?owner=xxxxxxxx" title="Schedule Appointment" width="100%" height="800" frameBorder="0"></iframe><script src="https://embed.acuityscheduling.com/js/embed.js" type="text/javascript"></script>...
The above URL of the iFrame is the general appointment scheduling of the owner xxxxxxxx of the Squarespace; but there are also more specific URLs, like <iframe src="https://app.squarespacescheduling.com/schedule.php?owner=xxxxxxxx&appointmentType=yyyyyyyy" title="Schedule Appointment" width="100%" height="800" frameBorder="0"></iframe><script src="https://embed.acuityscheduling.com/js/embed.js" type="text/javascript"></script>, which refer to a specific task yyyyyyyy of the owner xxxxxxxx...
So what I basically want is to be able, instead of creating 15 different pages, each with the appropriate yyyyyyyy task value, to create just one page withouth the appointmentType param in the iFrame's URL, and then pass somehow the param with the appropriate value, when calling the page itself.
Is this even possible though, or should I do it the hard/boring way of creating each page for each task? TIA
OK, this was relatively easy once I found the correct resource... So I'm sharing it here, in case anyone needs it in the future!
Basically it's all explained in this video: https://www.youtube.com/watch?v=CBLIWeSmz30
The only difference in my case was that probably the code of the iFrame was complicated enough, and I was hitting some escaping issue, resulting in the last part of the code (<script src="https://embed.acuityscheduling.com/js/embed.js" type="text/javascript"></script>) to getting lost...
So I had to deviate slightly from the suggested solution, and instead I followed the shortcode solution. So I added a shortcode with the following snippet:
add_shortcode('scheduler_iframe', 'scheduler_iframe_func');
function scheduler_iframe_func($atts)
{
return '<iframe src="https://app.squarespacescheduling.com/schedule.php?owner=xxxxxxxx&appointmentType=' . $atts['course'] . '" title="Schedule Appointment" width="100%" height="800" frameBorder="0"></iframe><script src="https://embed.acuityscheduling.com/js/embed.js" type="text/javascript"></script>';
}
From there, it was just a matter of setting up correctly the appropriate settings in Elementor. So for that, I replaced the HTML block with a Shortcode block, and instead of adding the shortcode in the appropriate field, I chose Request Parameter, then I set it up like in the images below:
Finally visiting the URL of that page and passing the appropriate ?course=yyyyyyyy querystring in the URL, was enough to fetch the appropriate iFrame that I wanted to show!

Creating new oEmbed video provider / oEmbed displays in Wordpress posts but not in the editor

We are creating a service which hosts videos. Users upload videos to our website, and we provide embed codes for playback on their websites.
We have a number of users currently using Wordpress, so we figured we would create an oEmbed service to let users easily copy/paste links into their posts.
We followed a tutorial, and and registered the following provider in wordpress via wp_oembed_add_provider in functions.php
wp_oembed_add_provider('https://vectorly.io/*', 'https://vectorly.io/wordpress/embed');
We set up an oEmbed service endpoint (https://vectorly.io/wordpress/embed?url=....), which returns the following json
{
"version":"1.0",
"type":"video",
"provider_name":"Vectorly",
"provider_url":"https://vectorly.io/",
"width":"600",
"height":"340",
"thumbnail_width":"600",
"thumbnail_height":"340",
"thumbnail_url":"https://s3-us-west-2.amazonaws.com/m.cdpn.io/screenshot-coming-soon-small.png",
"title":"NVP DemoDay - Vectorly",
"html":"<iframe src=\"https://api.vectorly.io/embed/demo/ba37b243-46c4-4f60-9a12-f6885b95c661\" width=\"600\" height=\"340\" frameborder=\"0\" allowfullscreen />"
}
Embedly seems to validate the output
Copying and pasting one of our links (https://vectorly.io/watch/ba37b243-46c4-4f60-9a12-f6885b95c661) into the Wordpress post editor does seem to successfully grab the oembed details
But the embed content editor block doesn't show a preview of the video
The actual published post looks fine though
It seems that the preview block is loading the iframe properly, but is just setting the width and height as 0 during the preview.
Looking at other non-whitelisted oembed providers, this issue doesn't seem to come up, so I think I'm doing something wrong, but I can't figure out for the life of me what it is.
Mucking around in various Wordpress files, I think that other oEmbed links (even from non-whitelisted 3rd parties) are doing something to trigger sizing of the preview Iframe in Javascript.
Any insight on this would be appreciated. Otherwise, we'll just forego oEmbed and stick with vanilla html embed codes
In case anyone else stumbles on this issue in the future:
You need to provide iframe close tags when you send oembed responses to wordpress
I.E. The following does not work
<iframe src="..." />
The following does work
<iframe src="..." ></iframe>
Adding a figure tag improves the result. See the fixed oEmbed output from our server
{
"version":"1.0",
"type":"video",
"provider_name":"Vectorly",
"provider_url":"https://vectorly.io/",
"width":"600",
"height":"338",
"thumbnail_width":"600",
"thumbnail_height":"338",
"thumbnail_url":"https://s3-us-west-2.amazonaws.com/vv-raw-videos/ba37b243-46c4-4f60-9a12-f6885b95c661.png",
"title":"NVP DemoDay - Vectorly",
"html":"<figure><iframe src=\"https://api.vectorly.io/embed/demo/ba37b243-46c4-4f60-9a12-f6885b95c661\" width=\"600\" height=\"338\" frameborder=\"0\" allowfullscreen ></iframe></figure>"
}

Changeable element shortcode

I'm building a website for a nutritionist in wordpress, where it has every patient area.
So I was thinking to make a section with a motivation youtube video and this code will be placed to every customer page.
Is there a way I can place for example [motivation_video] on the wordpress and that I could change the youtube link on my backend whenever I want? So I won't need to change all the pages.
Thank you so much!
Inside your wordpress functions.php file place the following code with your modifications:
function video_function($atts, $content = null){
$shortcode_return = '<iframe width="560" height="315" src="video_url_here" frameborder="0" allowfullscreen></iframe>';
return $shortcode_return;
}
add_shortcode('motivation_video','video_function');
Whenever you want a new video, just change the video_url_here

Wordpress (Pods) oEmbed Items

I'm using the fantastic Pods plugin to extend Wordpress's basic content types with a few custom ones. I've build an advanced custom type which means I don't get the automatic oEmbed support built into the native page/post types. I've structured it so my custom content type has a pod page using a PHP page template and I have the oEmbed option enabled for my WYISWYG fields that can embed videos.
I found this post which seems to indicate that a basic apply_filter function should automatically handle any embeds but I can't seem to get it to work. I'm a bit new to filters. The code I tried is below:
<?php
// Fetch body field content from $pods object
$mycontent = $pods->field('field_body');
$output = apply_filters('oembed_dataparse', $mycontent);
echo $output;
?>
I tried a variety of different filters such as the_content and others but none seemed to work. I believe it may be a scoping/conflict issue with Pod pages since even writing out entire iFrame embed code into the template won't work but only displays an empty iFrame. The global oembed function does the same, i.e.
$videourl = 'http://www.youtube.com/watch?v=dQw4w9WgXcQ';
$htmlcode = wp_oembed_get($videourl);
echo $htmlcode;
In the context of the page template will output:
<iframe width="500" height="375" frameborder="0" allowfullscreen="" src="http://www.youtube.com/embed/dQw4w9WgXcQ?feature=oembed">
<html>
<head>
</head>
<body>
</body>
</html>
</iframe>
field() gets the value of the field, display() gets the output of the field (ran through any related filters / functions the field is configured to run through).
$mycontent = $pods->field('field_body');
should be
$mycontent = $pods->display('field_body');
For more information, see http://pods.io/docs/field/ vs http://pods.io/docs/display/
Calling apply_filters('oembed_dataparse', $mycontent) is incorrect since this meant to add functionality for processing other data types (photo, video etc) not catered for by default. What you want to do is mimic how WordPress does the embedding. I haven't tested the code below, but it seems to me the way to go about triggering the embed functionality:
global $wp_embed;
$mycontent = $pods->field('field_body');
$output = $wp_embed->autoembed($mycontent);
echo $output;

Create an Iframe from a Drupal Website

I have a drupal website. I want to generate an Iframe with content from my drupal site, that other sites can embed.
How I think this can be achieved:
Method 1: Create a php script that is autonomous from the drupal engine.
Import the configuration file and hence gain access to the database. Generate the content as a standalone webpage. Distribute the url of this script as the iframe's source url.
Problems: cannot provide drupal functionality within the iframe such as interaction with logged in user.
Method 2: Create the iframe from within drupal.
Create a new module that defines a menu entry using hoom_menu (the url for the iframe). Define the content of the iframe from the callback function of the menu entry. Then Somehow assign a custom page.tpl.php theme for the desired iframe url so that only the content of the iframe is rendered without all the other page elements (blocks, menus, footer, etc.).
Any comments especially for method 2 will be greatly appreciated! :)
I have done exactly this, just this week!
I created a custom module that outputs only the content that I want (using hook_menu()). Then I created a page template (page-mycustommodule.tpl.php) that only has
<?php print $content; ?>
within the <body> tags.
That was basically all. To find out the name that your page template needs to have, use the devel and theme_devel modules, then just click on your page and it will tell you which templates it looked for.
One thing to look out for: any links in the iframe will only change the contents OF THAT FRAME, so when your module creates links, use the proper target to make the parent page jump to the new URL:
l('link text',
'node/' . $mynode->nid,
array('attributes' => array('target' => '_parent')));
I found the answer by Graham very useful. Unfortunately I don't have enough reputation on this site to add a comment so I will have to put my comment in an answer.
5 years on, the information has changed slightly:
The module theme_devel now seems to be called devel_themer instead.
In D7, the template naming pattern is slightly different with 2
hyphens: page--[front|internal/path].tpl.php (see docs)
D7 templates are slightly different based on render arrays, so the template will need to be something like print render($page['content']);
method 3
you can use this module https://www.drupal.org/project/entity_iframe that allows you to create IFRAME READY webpages
install it and go to the display settings of you content type that you want to use as iframe content admin/structure/types/manage/CONTENTTYPE/display
choose the IFRAME view mode and choose the fields you would like to be shown
and then use url like this domain.com/entity_iframe/node/NID and you will have a display with no extra headers footers etc ...
By default a sample EMBED/IFRAME code is provided to the admin under each node the settings
<iframe width="100%" height="300" src="domain.com/entity_iframe/node/96" frameborder="0" class="entity_iframe entity_iframe_node" id="entity_iframe_node_96" allowfullscreen="true" webkitallowfullscreen="true" mozallowfullscreen="true"></iframe>
The settings in admin/config/system/entity_iframe control some of the details of the embed code
For complete control of the theme used you can use in combination with https://www.drupal.org/project/entity_iframe_theme
What do you exactly need to iframe?
A node? A block? Should it be static or dynamic?
You can simply create a node with a php filter and generate the iframe output.
Then you can put this output between <pre> tags to display it as code that users can copy/paste in their site.

Resources