Quick Way to Include External Text in Wordpress

Here's a quick and simple way to include external text or HTML in Wordpress posts, pages, or widgets.

Posted by David Coleman on May 22, 2018

This snippet comes with a giant hat tip to mekanoid, which is where I got it from. But I thought it worth posting here with a little more explanation and also for my own reference for next time I need it.

Basically, what this does is insert the contents of an external html, txt, or markdown file into Wordpress.1 There are a number of plugins and other snippets that can accomplish something similar, but this is the option that's working well for me. There are no bells and whistles to this approach, but if you don't need those, this is a quick and easy option.

/* Add shortcode to include external content */
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' );

You can then insert it anywhere that can process shortcodes. I'm using it in a sidebar widget, but it also works in the main post content.

To use it, simply insert the shortcode in this format:

[show_file file="YOUR-URL.com/page.html"]

replacing the file location part to point to whatever file you want to insert.

In my case, I'm using this in a sidebar widget to pull in a bare-bones text file that I need to update quickly and regularly. It's much quicker for me to edit that file locally on my hard drive than open up the Wordpress dashboard's widget editing feature. I then have Syncovery monitoring that folder and uploading the new version of the file to a location on my server.

This is a bare-bones solution, and there are no parameters to set or internal control over the display. It will just take all the content and insert it. So if you're referencing an external html file, it will include all of that file's styling as well. It's not grabbing the content from inside the database--it's reading the publicly available html.

If you need something fancier that grabs the contents of another post on your own site, and does so from within the database, I recommend the RPS Include Content plugin, which I've written about before.

One final thing . . . While you can technically point at any publicly accessible html or txt file, there's no security mechanism or parsing built in to this--it's just going to blindly take the external content and insert it. So the safest option is to point only to resources you control rather than risk something in the external html causing problems on your site. In short: use at your own risk.


  1. If you try to insert a markdown .md file into a part of the page that isn't set up to parse markdown code (such as a widget area), it will be treated as a basic txt file.