Skip to content

Instantly share code, notes, and snippets.

@chrisguitarguy
Created February 7, 2012 02:52
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisguitarguy/1756834 to your computer and use it in GitHub Desktop.
Save chrisguitarguy/1756834 to your computer and use it in GitHub Desktop.
Embed your recent posts anywhere as an iframe!
<?php
/*
Plugin Name: iFrame Your WP
Description: Allow people to embed your latest posts in an iFrame
Author: Christopher Davis
Author URI: http://www.christopherguitar.me
License: GPL2
*/
register_activation_hook( __FILE__ , 'wpse32725_activation' );
/**
* Activation hook to flush rewrite rules
*
* @uses flush_rewrite_rules
*/
function wpse32725_activation()
{
flush_rewrite_rules();
}
add_action( 'init', 'wpse32725_add_rewrite' );
/**
* Adds the rewrite rule for our iframe
*
* @uses add_rewrite_rule
*/
function wpse32725_add_rewrite()
{
add_rewrite_rule(
'^iframe$',
'index.php?iframe=true',
'top'
);
// shortcode for our iframe
add_shortcode( 'iframe-code', 'wpse32725_iframe_code' );
}
add_filter( 'query_vars', 'wpse32725_filter_vars' );
/**
* adds our iframe query variable so WP knows what it is and doesn't
* just strip it out
*/
function wpse32725_filter_vars( $vars )
{
$vars[] = 'iframe';
return $vars;
}
add_action( 'template_redirect', 'wpse32725_catch_iframe' );
/**
* Catches our iframe query variable. If it's there, we'll stop the
* rest of WP from loading and do our thing. If not, everything will
* continue on its merry way.
*
* @uses get_query_var
* @uses get_posts
*/
function wpse32725_catch_iframe()
{
// no iframe? bail
if( ! get_query_var( 'iframe' ) ) return;
// Here we can do whatever need to do to display our iframe.
// this is a quick example, but maybe a better idea would be to include
// a file that contains your template for this?
$posts = get_posts( array( 'numberposts' => 5 ) );
?>
<!doctype html>
<html <?php language_attributes(); ?>>
<head>
<?php /* stylesheets and such here */ ?>
</head>
<body>
<ul>
<?php foreach( $posts as $p ): ?>
<li>
<a href="<?php echo esc_url( get_permalink( $p ) ); ?>"><?php echo esc_html( $p->post_title ); ?></a>
</li>
<?php endforeach; ?>
<ul>
</body>
</html>
<?php
// finally, call exit(); and stop wp from finishing (eg. loading the
// templates
exit();
}
function wpse32725_iframe_code()
{
return sprintf(
'<code>&lt;iframe src="%s"&gt;&lt;/iframe&gt;</code>',
esc_url( home_url('/iframe/') )
);
}
@franz-josef-kaiser
Copy link

Maybe you could add some predefined template tag, that holds the embed code inside an input field.

Something like

function show_embed_field()
{
    $embed_code = wpse32725_iframe_code();
    // look at the source in the following line: input gets stripped in comments
    return print '';
}

@chrisguitarguy
Copy link
Author

There is a shortcode there to stick the embed in a page. Maybe I should mimic the WP core: function to fetch the stuff and another to echo it out?

What do you mean by this?
// look at the source in the following line: input gets stripped in comments

@franz-josef-kaiser
Copy link

Ah, I overread the shortcode - mea culpa.

@look at the source: Github strips the part after return print in the comment in my code lines above :)

@get_* and the_*: absolutely. A public API with "template tags" is always appreciated. If I'd use your plugin, i'd write one just to avoid the need to add the shortcode to every post I publish. Just check for the active plugin and then execute right before the comments loop.

@franz-josef-kaiser
Copy link

And one thing that would be kool as well is a hook instead of a post list definition inside wpse32725_catch_iframe().

@yjukaku
Copy link

yjukaku commented Sep 30, 2014

Did you ever submit this to the WordPress plugin repo?

@PrafullaKumarSahu
Copy link

PrafullaKumarSahu commented Apr 12, 2017

I tried your code, but accessing http://localhost/mysite/iframe giving 404 , Can you please give me some idea, how I can make it work ?
UPDATE working for http://localhost/mysite/?iframe=true.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment