Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
burger menu icon
WillMaster

WillMaster > LibraryWordPress Widgets & Other WP Software

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

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

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC