Central Content File for Many Sites
As an owner of multiple websites, you undoubtedly have had to make the same page edit on each site. Perhaps there was a telephone number change or a we-are-on-vacation notice.
It is possible to update one central file with all websites updating themselves automatically. It's an efficient way to do things.
With the ease of implementation described in this article, you may decide to add some intermittently-changing information to pages at your various websites. When the information changes, only one file needs to be updated.
The information might be (as examples):
-
Local weather.
-
Information with link about your latest blog post.
-
Special menu links. (We use it for this. You'll see them in the right-hand column of the Blog and Library pages.)
-
Local ads. (We use it for this. As of this writing, it's an ad for 52 Good-to-Know Techbits.)
-
Project progress notes.
-
Contact information.
What You Do
Implementation is two steps:
-
Make a file with the content and note its URL.
-
Insert a line of PHP code on pages where the content is to be published.
Step 1.
Make one text file of the information to be inserted into web pages at your various sites.
The content needs to be HTML formatted. It may contain images, a form, a video, virtually anything that can be published on a web page.
Upload the file to one of your websites. (This will be the central file you update to change all web pages where the information is published.)
The uploaded file name can have any valid web page file name extension. If it has PHP code, .php
should be the extension.
Make a note of the uploaded file's URL.
Step 2.
Insert this line of PHP code in the web pages where the content in step 1 is to be published (customization note follows):
<?php echo(file_get_contents('http://example.com/page.php')); ?>
Customization:
Replace the http://example.com/page.php
URL with the URL of the file you uploaded in step 1.
Wherever the above PHP code is inserted, the content of step 1 is published …
… with one caveat. The PHP configuration on the server with the domain that is pulling in the content must allow URLs to be used in the file_get_contents() function. (Technical allow_url_fopen details.)
If you're unsure whether or not the PHP configuration on your site allows URLs to be used in the file_get_contents() function, test the PHP code on a temporary web page. Most do. But if yours doesn't, Ajax can be used. It would be more complicated to implement and it's outside the scope of this article. But it's noted to inform that there is a way.
Having only one central file to update to affect the content of numerous locations is a good thing.
You save time. And you eliminate all chance of forgetting the locations of all those pages that need updating.
As a bonus, the system is easy to implement.
(This article first appeared in Possibilities newsletter.)
Will Bontrager