No-Plugin External Content for WordPress
This article is specifically for WordPress users who desire to publish content from an external source without using a plugin.
Reasons for doing so:
-
To publish the same content in various places at a WordPress site.
-
To publish the same content at multiple WordPress sites.
When you make a change in the external file, the change is immediately propagated to everywhere the external content is used.
Here are a few examples of external content that might be used in this way.
-
The copyright line that generally is published at the bottom of web pages and that, again generally, is changed every year.
-
Contact information, perhaps when the email address might be changed from time to time as the current one starts getting a lot of spam.
-
Project progress notes. (Images and such can be included within the external content being imported.)
-
Sale items or prices that change from time to time.
What you do to implement this is publish content in a file at one of your domains. And also upload a PHP script that knows where to find your content file.
Then, put a script
tag wherever you want to insert the content. The script
tag can be in posts, in pages, and in "Text" or "Custom HTML" widgets for sidebars and footers.
Implementation Details
The external content file and the PHP script are installed on one domain. Then, the content file can be published with a line of JavaScript in a WordPress post, page, or widget at any domain.
The External Content File
The external content file is a PHP file, one with a .php
file name extension.
The external content file can have any content web pages can have — text, HTML markup, videos, tables, images, even iframes. Be aware that any JavaScript or PHP code in the external content file will run before
it is imported into a WordPress page, post, or widget.
Upload the content file to your server. Make a note of its URL.
The PHP Script
The source code of the PHP script is below. Details about the one customization follows the source code.
<?php
/* External File Into WordPress Content
Version 1.0
December 8, 2020
Will Bontrager Software LLC
https://www.willmaster.com/
*/
/* Customization */
// Specify the http:// or https:// URL of the external content file.
$ExternalContentURL = "https://example.com/content.php
";
/* End of customization */
header('Content-type: text/javascript');
foreach( explode("\n",str_replace("\r",'',file_get_contents($ExternalContentURL))) as $line )
{
$js = str_replace("\\","\\\\",$line);
$js = str_replace("'","\\'",$js);
$js = str_replace("<!--","<'+'!--",$js);
$js = str_replace("-->","--'+'>",$js);
$js = preg_replace('/(scr)(ipt)/i','$1\'+\'$2',$js);
$js = preg_replace('/(win)(dow)/i','$1\'+\'$2',$js);
$js = preg_replace('/(doc)(ument)/i','$1\'+\'$2',$js);
$js = preg_replace('/(text)(area)/i','$1\'+\'$2',$js);
$js = preg_replace('/(fo)(rm)/i','$1\'+\'$2',$js);
$js = preg_replace('/(vi)(deo)/i','$1\'+\'$2',$js);
echo "document.writeln('$js');\n";
}
?>
The customization —
Replace https://example.com/content.php
with the http:// or https:// URL of the content file on your server.
Then save the PHP script as external.php
(or other file name ending with .php
). Upload external.php
to the server. Make a note of its URL.
(Although external.php
could run on a different domain, uploading it to the domain where the external content file is at, perhaps even in the same directory, is a way to keep things more intuitively organized.)
Publishing the Content in WordPress
To publish the external content into WordPress posts, pages, and/or widgets, use this line of JavaScript as customized according to the instruction that follows.
<script src="https://example.com/external.php
"></script>
Customize the JavaScript by replacing https://example.com/external.php
with the http:// or https:// URL of the external.php
PHP script that you uploaded in the previous step.
Now that the line of JavaScript is customized, it can be used to pull in the content for publishing in WordPress.
For publishing the external content into a post or page, use a "Custom HTML" content block. Paste the customized JavaScript into the block.
For inserting a widget on your pages with the external content, use a "Custom HTML" widget. Paste in the customized JavaScript into the widget. (The widget area or areas that are available depend on your theme.)
You are now all set to insert the content of your external file in WordPress content at any domain.
(This article first appeared with an issue of the Possibilities newsletter.)
Will Bontrager