How to Include External Content on Your WordPress Site

Featured image by 200 Degrees from Pixabay

Here’s the setup…  Your job is to administrate multiple WordPress websites each of which is on a different domain, has a different set of plugins installed, different themes, the works.  However, there is a relatively small bit of content that you need to include on every site.  Let’s pretend that’s a terms and conditions statement.  Not only that, but these terms and conditions get updated every so often.  What do you do?

Well, if you only have a handful of sites it’s likely that you’d rewrite or edit your terms and conditions once and then copy / paste it a few times into the small number of sites you manage.  How about if you have a dozen sites?  Thirty?  Fifty?  Eleventy billion?  This copy / paste method will get real old, real quick.

Much like the ibuprofen I almost have to take before bed these days (because I’m old), I’m here to alleviate that pain for you.

My Brand of Ibuprofen

I have this exact problem and with the help of David Coleman’s post, I was able to solve it quickly and easily.  It involves three simple steps.

  1. Create a text file on a host domain.
  2. Insert a small bit of code into the functions.php file on the target domain.
  3. In some page or post on the target domain, insert a shortcode that links to the file on the host domain.

This whole process should take 5 to 10 minutes (not including writing the content for the shared file).

Step #1 — Create a text file on the host domain.

Fire up your favorite text file editor and get to work.  This is the sample Terms & Conditions file I wrote for this demo.

<h1>Terms and Conditions</h1>
<p>These are our terms and conditions. They are very important and you should read them thoroughly. Our terms are very term-like and our conditions are highly conditional.</p>
<p>This is a list of terms and conditions.
<li>Term #1</li>
<li>Condition #1</li>
<li>Term #2</li>
<li>Condition #2</li>
<li>Term #r</li>
<p>These terms and conditions are non-negotiable and binding.</p>

Notice I wrote this as HTML.  The beauty of this method is that anything goes.  If you want plain text, HTML, PHP, or any other kind of markup, just write it.  It should be rendered correctly.  (Note, I have not extensively tested this so there are probably limitations.  I’ve noted below a couple of limitations that I’ve found in my brief testing.)

Now take this file and upload it to your host domain.  I suggest using FTP or a file manager plugin for the upload.  You could upload it to your Media collection but then editing the file would be a challenge.  For this demo, we’ll put the file in (Notice that I named the file “.txt.”  Pay attention to that.  More on that later.)

Another choice would be to upload it to your theme directory and then you can edit it directly from the WordPress Admin Console.

Step #2 — Insert a small bit of code into the functions.php file on the target domain.

I didn’t invent this code.  That’s thanks to David (linked above).  Regardless, drop these few lines of code into the functions.php file of your active theme.  It creates a shortcode and grabs the data from the remote file.

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

This is PHP so make sure it’s inside any “<?php” or “?>” tags.

Step #3 — In some page or post on the target domain, insert a shortcode that links to the file on the host domain.

This is, perhaps, the most simple part.  Find the page or post on which you want to include the remote content and place the following shortcode:

[show_file file=""]

Obviously, you will need to replace the file location I’m showing here with your own uploaded file location.

That’s It!

If you did it all correctly, then whatever you’ve got in your text file will now display on the page / post where you have the shortcode.  If not, go ask David about it.  😉

A Few Limitations I’ve Found…

  1. Security — There is none.  I’m sure this breaks half a dozen website security rules.  I’m not sure what those rules are or how to patch those holes but use this trick at your own risk.
  2. Child Theme — Since you are modifying your theme’s function.php file you should create a child theme before you do anything I show you here.  If you do not then your work will be erased by the next theme update that comes along.
  3. The “rel” Attribute — I had a really difficult time trying to figure out why I couldn’t put both rel=”noopener” and rel=”nofollow” in my text file and have them display on the target domain.  The first one would come through but not both.  I never did figure this out…but I did figure out that you can use rel=”nofollow noopener” and it works fine.  So, problem mostly solved.
  4. File Extensions — I used .txt as my file extension in this example and it works fine.  You can even use HTML markup in the file and that gets parsed & displayed correctly.  If you add PHP to the .txt file, it will come through as plain text but it will be hidden on the page / post (although it will be visible in “view source”).   If you change the file extension to .html there is no change in behavior.  If you change the file extension to .php then any PHP in the file will be executed correctly on the host domain.  The PHP is executed on the host domain, the resulting information is sent along with everything else in plain text, and it’s displayed on the target site.  I have not yet found a way to send the PHP commands as plain text and have them executed on the target site.

Your Turn

If you have ideas about how to extend this or tweak it to make it better, I’d love to know.  Send me a note or leave a comment!

Jerod Karam

Jerod Karam is Vice President of Technical Operations at Netvantage SEO, an online marketing company specializing in SEO, PPC and social media. Jerod consults with internal teams and external clients on all manner of technical projects, manages the flow of information surrounding the company's online objectives, manages relationships with external partners and suppliers, and is a constant bother to everyone in terms of maintaining online security.

Leave a Reply

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