How to include text from a single source on websites
Sometimes you need to keep a master copy of some text on a website that just updates. For example, if you have a group of terms and conditions across multiple sites then it would be easier to update once rather than on each site.
I needed a solution as Bet365 are always updating their terms and need me to keep them up to date. They provide an auto-updated iframe of the terms in the backend. It works fine in the main body of a post but in the sidebar, it doesn't show. So I needed to find another way.
I ended up using the code on this site
/* 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' );
Add that to functions.php in the theme files. Ideally, you'd have a child theme and put in there.
to use just add this.
[show_file file=”YOUR-URL.com/page.html”]
I actually use a plugin called shortcoder, it allows me to update shortcodes on the site quickly. Very useful for banner management. So I can paste the code into the terms shortcode I have already set up. I was all very simple in the end.
There may well be a plugin that does allow you to reference external text in a more user-friendly way but I couldn't find it. The RPS include plugin referenced here only works within a site, not externally. Would be handy to have like a CDN for text snippets but I can't find one either. I use cloudinary.com for some banner management but there appears to be nothing for txt. Maybe because text is so simple, there's not much demand for a solution.
Ben has a BEng (Hons) in Computer Science and 20 years of experience in online marketing, specialising in SEO, lead generation and affiliate marketing. After spending over a decade as an igaming affiliate, he has decided to concentrate on GA4 training and SEO Audits.