Software, your way.
burger menu icon
WillMaster

WillMaster > LibraryMarketing With 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!

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:

  1. Distributing your articles.

  2. Publishing ads at other sites.

  3. 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.

  1. You — You install a PHP script (SyndicatePage.php, provided in this article) and you create a web page with content for syndicating.

  2. 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:

  1. Replace SCRIPT_URL with the URL to your installation of the SyndicatePage.php script.

  2. 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

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