X

Include External File Shortcode

Snippets by IsItWP

External files are used to display external content on your WordPress website. With the help of a shortcode, you can easily achieve it.

Instructions: Add the following code to the functions.php file of your WordPress theme to include external file shortcode.

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' );

Now you can use this shortcode given below with your external file link to include the external file in your WordPress posts and pages.

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

You may also enjoy creating a file upload form in WordPress.

Comments  Leave a Reply

  1. It’s done. Thank you very much.

    1. Glad it helped, Baba!

  2. WP variables aren’t recognized in the included file. Also tried $_GET, $_POST but to no avail. How to fix this?..

    1. This is not something that I have tried to do, however I’m not sure why you would want to include WP variables when you could do this within the post or using the functions.php and shortcode. What is it that you are trying to do, this may help me understand the best option.

  3. Chris Olbekson August 6, 2013 at 2:52 pm

    This snipp has serious security implications. It could allow any editor or user to access and read the contents of any file on the server. You could use the WordPress built in HTTP API and use wp_remote_get instead of file_get_contents().

  4. Good

  5. Its sounds good but how to include code that works ? I mean, I have a nextgen gallery stuff I would include this way, but shortcode does not works (it show nextgen shortcode and not the thumbnails) also plugins does not works (such as auto-hyperlink, even css does not works) on the remote included file’s contents.

  6. very nice!

  7. Will this work in a widget area?

    1. You would need to enable shortcode for widgets, eg:

      add_filter(‘widget_text’, ‘do_shortcode’);

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!