Add to Technorati Favorites

Wordpress Frame Breakout

So your WordPress blog has been linked from someones website and they have it trapped in a containing frame…

Using a simple snippet of javascript within your header.php works nicely untill you decide to edit or create a new page or post.

You soon discover that the same piece of javascript in your header will prevent you getting very far as WordPress uses an iframe to display a nice preview of your work and the code breaks out of this also.

I have put together this lil snippet of code, it should work with most browsers and should be included in your header.php template between your <head></head> tags and above your <title></title> tag.

<!-- Frame breakout -->
<?php
$myreferer = explode( "/", $_SERVER['HTTP_REFERER'] );
$mybloginf = explode( "/", get_bloginfo('url') );
if ( $myreferer[2] != $mybloginf[2] ) {
?>
<script type="text/javascript">
if (self != top) {
top.location.href = self.location.href }
</script>
<?php } ?>
<!-- End Frame breakout -->

How about including a nice alert box that will pop up with a customised message…

This can be done easily by adding an extra line within the script section of the code:

<script type="text/javascript">
if (self != top) {
alert("I think frames are old and moldy\\nI want to break free!");
top.location.href = self.location.href }
</script>

Customise this message however you like and use \n to indicate a newline.

Created by: Martin Guppy
Created on: Thursday, January 25th, 2007 - 2:37 pm
Last Modified: Saturday, December 29th, 2007
Post Tags: , , 4 Responses
Post Rating: 1 Star2 Stars3 Stars4 Stars5 Stars (Be the first to rate this post)

4 Responses to “Wordpress Frame Breakout”

deadpan110 UNITED KINGDOM

Oops, I made a mistake In my code…

$myreferer = explode("/",$_SERVER['HTTP_REFERER'] );
if ( $myreferer[2] != get_bloginfo('url') ) {

Should read:

$myreferer = explode( "/", $_SERVER['HTTP_REFERER'] );
$mybloginf = explode( "/", get_bloginfo('url') );
if ( $myreferer[2] != $mybloginf[2] ) {

Now fixed within the post above!!

Posted: 3 February 2007 at 9:43 pm
Using: Mozilla Firefox Mozilla Firefox 1.5.0.9 on Linux Linux
Nicholas Weightman UNITED KINGDOM

Would that person who trapped it within frames be me? lol Well - I’ve since changed a few of the links with Target=”_Blank” to open it into its own window.

:D

Posted: 9 March 2007 at 3:17 pm
Using: Mozilla Mozilla 1.7.5 on Windows Windows XP
deadpan110 UNITED KINGDOM

Heh, glad you like it…

It is worth noting that in the modern world of web design that using frames in certain ways can have its downsides and if possible, the container frames should be controlled by a server side script (PHP, CGI etc) as the only main problem with frames is indeed navigation.

Not allowing people to bookmark a specific page or send links to others as any page they are viewing will always have the same URL in the browser address bar can be an annoyance.

It is unfortunate that some hosting providers still do not allow what could be considered as the norm and allow PHP and CGI in their most basic of web hosting solutions.

Using <a href="http://www.domain.com/" target="_blank">External Link</a> will indeed cause the users browser to open up the link within a new tab or browser window but can also be considered as an annoyance due to the fact that web design etiquette should let the user decide if they want to view a link in another window or tab and should not be forced to stay on your own site unless they choose to do so.

I certainly believe that ‘frames are old and moldy’ but I also believe that any site that is easy to navigate and well designed is important regardless of how the content is delivered.

Besides, both myself and Gurgi like your site Nicholas - keep up the good work :)

Posted: 10 March 2007 at 1:39 pm
Using: Mozilla Firefox Mozilla Firefox 2.0.0.1 on Linux Linux
Nicholas Weightman UNITED KINGDOM

As you probably know I’m not a PHP programmer - but I was asked recently by Reject to help setup a website for him. I decided to go with the wordpress system and after jazzing it up she asked if it was possible to display a picture for the person who posts an entry. I ofcourse went straight to work and come up with this. Its not perfect but it does the job and I thought someone else might find it useful if they have multiple users on there wordpress system and they want to display a picture of the person writing it.

Create Postby.php and put it in the directory of the theme you are using and add to the bottom of post.php (or move it up and down post.php until you get the location right)

postby.php
~~~~~~~
“;
}

elseif (get_the_author_login()==”user2″)
{
echo “”;
}

elseif (get_the_author_login()==”user3″)
{
echo “”;
}

else {
echo “”;
}
?>

~~~~~~~~~~
Just change the “user” with the username they use and ofcourse the picture and location to what they want.

Well I know its REALLY simple, but bare in mind I’m just learning bits and peices of PHP so its all new to me. I’m pretty proud of writing it - I just needed to find the function name for “get_the_author_login()” and once I got that it just all fell into place.

:D

Posted: 14 April 2007 at 12:10 pm
Using: Internet Explorer Internet Explorer 7.0b on Windows Windows Vista

Leave a Response...

Note: Will not be published