Quick Way to Include External Text in WordPress Posts & Pages

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

Computer Keyboard
Last Updated:

This post may include affiliate links. Read more.

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.

It’s something that I’ve come to use a lot on my WordPress sites where I want to be able to update some frequently updated information quickly in a local text editor without firing up the WordPress dashboard. Things like short news snippets or deals on camera gear and software.

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, and it doesn’t require adding another plugin.

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.

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

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

You’ll need to replace 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 that’s important: 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. That’s potentially a giant security hole.

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. There’s presumably a way to limit the filetype to, say, .txt files, but that’s not something I’ve needed or explored.

  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.[]
David Coleman / Photographer

David Coleman

I'm a professional freelance travel photographer based in Washington DC. Seven continents, up mountains, underwater, and a bunch of places in between. My images have appeared in numerous publications, and you can check out some of my travel photography here. More »

Leave a Comment