DISCLAIMER: this post is older than one year and may not be up to date with latest WordPress version.

When blogging, you may need to include an external file, such as text or image, into your own posts. Here is a very useful shortcode to embed and display any external files in your blog posts.

Open your functions.php file and paste the following function in it:

function show_file_func( $atts ) {
  extract( shortcode_atts( array(
    'file' => ''
  ), $atts ) );
 
  if ($file!='')
    return @file_get_contents($file);
}
 
add_shortcode( 'show_file', 'show_file_func' );

Once done, you can use the shortcode. It’s pretty simple:

[show_file file="http://www.test.com/test.html"]

Thanks to Vladimir Prelovac for the handy tip!

3 Comments

  1. I just finished a custom plugin that fetches remote content and stores the data in shortcodes. The problems with the solution in this snippet is that you will end up with a new doctype, <html> <head> and <body> tags in the middle of your document. Also, any inline stylesheets or unclosed HTML tags will break your layout. This is an easy way to include the output of a script you control, but if you need to include a whole page you’ll end up with a one or two hundred line solution like I did.

  2. This is a very dangerous shortcode that could allow authors to read any file on the server filesystem. You can make it safer by using WP’s HTTP api instead of file_get_contents(), but it’ll still be dangerous.

    [ show_file file=”/etc/passwd” ] !

  3. Interesting, Is it possible to use this Shortcode to embed the remote file into the sidebar instead of a post?

Leave a Comment

Your email address will not be published. Required fields are marked *