JavaScript Syndication
All or part of a web page can be syndicated to other web pages with JavaScript. Syndication can be to your own websites and/or to other people's websites.
Reasons to syndicate include:
-
Distributing your articles.
-
Publishing ads at other sites.
-
Send a daily saying or other custom content to many web pages.
The content to be syndicated is at a URL.
How It Is Set Up
There are two parties to make this work.
-
You — You install a PHP script (
SyndicatePage.php
, provided in this article) and you create a web page with content for syndicating. -
Site administrator of a web page — A line of JavaScript is inserted into the web page where your content is to be published.
Your content has specific "begin" and "end" marks to indicate the beginning and end of the syndicated content section of the web page. The marks (which may be customized) are <!--BEGIN_SYNDICATION-->
and <!--END_SYNDICATION-->
.
For security, no web page content without your specified "begin" and "end" marks can be syndicated with this system.
How It Works
The line of JavaScript on the destination web page calls SyndicatePage.php
on your server and provides the URL of your web page.
The SyndicatePage.php
script grabs the page at the URL. It strips out any content before the "begin" mark and after the "end" mark. Then it sends the content that's left to the destination page.
Example Web Page With Content for Syndication
This source code is for a web page on your website with content that may be syndicated.
<!doctype html>
<html>
<head>
<title>Has Content for Syndication</title>
</head>
<body>
<p>
This part won't be syndicated.
</p>
<!--BEGIN_SYNDICATION-->
<p>
I am the content available for syndication.
</p>
<!--END_SYNDICATION-->
<p>
Neither will this part be syndicated.
</p>
</body>
</html>
In the source code of the above web page, this will be the only part that can be syndicated:
<p>
I am the content available for syndication.
</p>
The SyndicatePage.php
Script
Below is the source code for the PHP SyndicatePage.php
script. Below the source code is a comment about one optional customization that may be made.
<?php
/*
Syndicate Page
Version 1.0
February 15, 2021
Will Bontrager Software LLC
https://www.willmaster.com/
*/
/* Two values that may require changing: */
$SyndicationBegin = '<!--BEGIN_SYNDICATION-->';
$SyndicationEnd = '<!--END_SYNDICATION-->';
/* End of customization section */
$synbegin = preg_quote($SyndicationBegin,'/s');
$synend = preg_quote($SyndicationEnd,'.*$/s');
$syndication_page = '';
if( ! empty($_GET['source']) ) { $syndication_page = file_get_contents($_GET['source']); }
if( ! ( preg_match("/$synbegin/",$syndication_page) and preg_match("/$synend/",$syndication_page) ) )
{
echo "document.writeln('Either the web page was missing codes or they were incorrect. Or there was no web page.')";
exit;
}
$syndication_page = preg_replace("/^.*$synbegin/s",'',$syndication_page);
$syndication_page = preg_replace("/$synend.*$/s",'',$syndication_page);
foreach( preg_split('/[\r\n]+/',$syndication_page) 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);
echo "document.writeln('$js');\n";
}
?>
Optional customization —
In the above source code, you'll see these lines:
$SyndicationBegin = '<!--BEGIN_SYNDICATION-->';
$SyndicationEnd = '<!--END_SYNDICATION-->';
That is where the "begin" and "end" marks are specified. Those marks are used in the web page to specify where the syndicated content begins and where it ends.
The "begin" and "end" marks may be changed in the script. Note that, if changed in the script, corresponding changes need to be made in all of your web pages that contain content to syndicate.
Uploading SyndicatePage.php
Upload the SyndicatePage.php
script to a location on your server with public access. Make a note of its URL.
The Syndication JavaScript
At the destination web page where your content will be syndicated, they have a line of JavaScript. Here is the format:
<script src="SCRIPT_URL?source=PAGE_URL" type="text/javascript"></script>
In the above JavaScript:
-
Replace
SCRIPT_URL
with the URL to your installation of theSyndicatePage.php
script. -
Replace
PAGE_URL
with the URL to your web page with content that may be syndicated.
Now, wherever that JavaScript is inserted into a web page, your syndication content is inserted.
If the web page being requested doesn't have both the "begin" and "end" syndication marks, the SyndicatePage.php
script will send back an error message.
You are now set up to syndicate your content.
(This article first appeared with an issue of the Possibilities newsletter.)
Will Bontrager