Software, your way.
burger menu icon
WillMaster

WillMaster > LibraryWebsite Development and Maintenance

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!

Instant Web Page Section Updates — Even Sitewide

It's possible to update a section of your web pages at any time — site wide.

The section of your web pages is updated from a file you maintain separately. Whenever the file is changed, that section of your web pages also changes.

Think of it as syndicating content — publishing content simultaneously on a small section of some or all your web pages. As examples, it might be a quote, a deadline notice, an ad, a price, an announcement, or a series of live updates from an event.

The syndicated content is in a separate file, perhaps on the same domain or provided by another domain you control.

The section on your web pages containing the syndicated content can be updated instantly. "Instantly" means as soon as the content of the separate file is updated, the very next page load will publish the updated content.

(If you prefer not to manually maintain the file, see near the end of this article for software suggestions.)

Implementation Instructions

When the syndicated content file is ready, make a note of its URL. There are several ways to pull it in for publishing on your web pages.

  1. With PHP — If you're able to use PHP on your web pages, one line of PHP code will pull in the content.

    <?php echo(file_get_contents("http://example.com/content.html")); ?>
    

    Replace http://example.com/content.html with the URL of the file containing the content to syndicate. Insert the PHP code in the source code of your web page where you want the syndicated content to be published.

    You're good to go. The rest of this article can be ignored.

  2. In WordPress Posts and Pages — For WordPress posts and pages, you can use the Insert Here plugin. After installing the plugin, insert the plugin short code into your web page where you want the syndicated content to be published.

    [insert_here url="http://example.com/content.html"]
    

    Replace http://example.com/content.html with the URL of the file containing the content to syndicate.

    You're good to go. The rest of this article can be ignored.

  3. In WordPress Sidebar or Footer Widget Areas — Follow the instructions leading from the "All others" method mentioned next. Except instead of putting the JavaScript into your web page source code, you put it into a WordPress text widget.

  4. All others. — JavaScript pulls the syndicated content into your web page via a PHP script. The PHP script converts the content into valid JavaScript code.

JavaScript < PHP Script < Syndicated Content File

The JavaScript pulls in the syndicated content via a PHP script.

The PHP script responds with the syndicated content converted into valid JavaScript code.

Here's the PHP script source code. (No customization required.) Comments follow.

<?php
/*
   Text to JavaScript Converter
   Version 1.0
   April 18, 2016

   Will Bontrager Software LLC
   https://www.willmaster.com/
   Copyright 2016 Will Bontrager Software LLC

   This software is provided "AS IS," without any warranty of any kind, without even 
   any implied warranty such as merchantability or fitness for a particular purpose. 
   Use of this software is subject to the Website's Secret Software License Agreement.
   https://www.willmaster.com/software/WebSitesSecret/WS_LicenseAgreement.html
*/
header('Content-type: text/javascript');
if( empty($_SERVER['QUERY_STRING']) ) { echo JS('No URL was provided.'); exit; }
foreach( preg_split('/[\r\n]/', trim( file_get_contents( urldecode($_SERVER['QUERY_STRING']) ) ) ) as $line ) { echo JS($line); }
exit;
function JS($line)
{
   $js = str_replace("\\","\\\\",$line);
   $js = str_replace("'","\\'",$js);
   $js = str_replace("<!--","<'+'!--",$js);
   $js = str_replace("-->","--'+'>",$js);
   $js = preg_replace('/(fo)(rm)/i','$1\'+\'$2',$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);
   echo "document.writeln('$js');\n";
}
?>

Upload the PHP script to your server as GetThisForMe.php or another .php file name that works for your implementation. (The rest of these instructions assume the file name is GetThisForMe.php)

Make a note of GetThisForMe.php's URL.

Now that the PHP script is installed, here's the JavaScript for publishing the syndicated content into your web page:

<script src="https://example.com/GetThisForMe.php?http://example.com/content.html"></script>

There are two customizations for the JavaScript:

  1. Replace http://example.com/GetThisForMe.php with the URL of GetThisForMe.php on your server.

  2. Replace http://example.com/content.html with the URL of the file containing the content to syndicate.

The JavaScript is now ready to paste into your web page.

If the JavaScript is for a WordPress sidebar or footer, put it into a WordPress text widget.

Otherwise, paste the JavaScript into the web page source code where the syndicated content is to be published. That can be accomplished by either —

  1. inserting it directly into the source code of the web page when only one or a few pages will carry the content, or

  2. pasting the JavaScript into a template that's used to generate your web site, putting the section with the content into all of your pages.

You are good to go.

(This article first appeared in Possibilities ezine.)

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