Daily Content for Many Websites
Daily content can be served to your web pages, any web pages, including WordPress pages and posts, from one central distribution point.
The same content is served every day at all destinations. The next day, different content. Consistently. (In general, those statements are true. On days you add or remove daily content files, the content being published for the current day may change.)
The daily content may be any content that can be published on a web page.
As examples, the content might be recipes, reviews of certain types of products, political themes, or pertinent sayings.
If you have more then one website, the same content can be published on your other sites. The content resides in one directory on one domain and is served to other websites from there.
You may invite website owners to publish your content, too. If you have similar websites or have similar personal interests or concerns, one website may provide daily content for all who are interested.
When it comes right down to it, what you are doing is syndicating your daily content to one or more websites.
The daily content is public (like most content on the internet). Anyone can get the content. That is by design, so you can publish on any of your websites and so you can invite other site owners to publish your daily content on theirs.
If you prefer to restrict content to only your one website, see 5-Line PHP Script to Rotate Content Daily.
Getting Started
To get started, you'll need a dedicated directory on your domain server. The dedicated daily-content directory will contain 2 types of things.
- The daily content files.
- The PHP script to deliver the content.
No other files should be in the daily-content directory. However, the PHP script ignores subdirectories. So you may give the daily-content directory one or more subdirectories for other files.
When the daily-content directory is ready, you publish the daily content with a bit of HTML and JavaScript (which, optionally, may be all one line).
The Daily Content Files
Create an exclusive directory for the daily content files. This article assumes /dailycontent
is the directory name, but you may choose a different one.
Daily content files may contain any content that can be published on web pages — one file per day. The content of the file for the day is inserted into a web page as regular web page source code. Unless the content has appropriate HTML markup, like a regular web page would have, it will tend to be published all in one blob.
Like regular web page source code, to publish an image, video, or other non-text items, use HTML markup to do it.
The content file names need to have an acceptable file name extension. Examples: .html .htm .php .txt
If the daily content is in a .*php
file and contains PHP code, the PHP code will run before the content is delivered and published. As examples, PHP may be used to publish the current date and time or to insert an ad.
Put the daily-content files into the /dailycontent
directory.
The Content-Delivering PHP Script
The PHP script (source code further below) determines which content to deliver on the current day and then delivers it.
No customization is required. Simply save the source code as syndicate.php
and upload it into the daily-content directory. You may name the script file something else that ends with .php
but this article assumes syndicate.php
is the file name.
Make a note of its URL. You will need it for the code to publish the daily content on a web page. For this article, we'll assume the URL is:
https://example.com/dailycontent/syndicate.php
Here is the source code.
<?php /* Syndicate Daily Content Version 1.0 July 15, 2022 Will Bontrager Software LLC https://www.willmaster.com/ */ header("Access-Control-Allow-Origin:*"); $G = array(); $G['selfURL'] = ((isset($_SERVER['HTTPS']) and $_SERVER['HTTPS']=='on')?'https://':'http://') . $_SERVER['HTTP_HOST'] . (preg_replace('/\s/','%20',$_SERVER['PHP_SELF'])); $G['selfFile'] = preg_replace('!^.*/!','',$_SERVER['PHP_SELF']); $G['selfURL2file'] = preg_replace('![^/]*$!','',$G['selfURL']); if( isset($_GET['id']) ) { DeliverAjax(); } else { DeliverDailyContent(); } exit; function DeliverAjax() { global $G; echo <<<AJAX function _getThisDaysContent(){ var http = new XMLHttpRequest(); http.onreadystatechange=function(){if(http.readyState==4 && http.status==200){document.getElementById("{$_GET['id']}").innerHTML=http.responseText;}} http.open("GET","{$G['selfURL']}"); http.send(); } _getThisDaysContent(); AJAX; } # function DeliverAjax() function DeliverDailyContent() { global $G; $files = array(); foreach( glob('*') as $f ) { if( (!is_file($f)) or preg_match('/^\.|\.\.|'.preg_quote($G['selfFile'],'/').'$/',$f) ) { continue; } $files[] = $f; } $numfiles = count($files); if( ! $numfiles ) { echo 'NO FILES AVAILABLE'; } else { echo( file_get_contents($G['selfURL2file'].$files[(intval(date('zY'))%$numfiles)]) ); } } # function DeliverDailyContent() ?>
The syndicate.php
script determines which content to deliver and then delivers it.
There is nothing that you have to do to mark the content for the day it will be published. It's all taken care of automatically.
How syndicate.php
determines which content to deliver —
Understanding this section is optional. Read only if you're interested in how the software determines which file is the day's content file.
A modulo math operation is done. The operation finds the remainder when one integer is divided by another.
The first integer is the current day of the year with the current year number appended. Today being November 21, 2024, the day of the year is 325. With the year 2024 appended, the constructed number becomes 3252024.
The second integer is the number of daily-content files in the /dailycontent
directory. For this illustration, we'll assume there are 14 daily-content files.
After 3252024 is divided by 14, the remainder is 6. The number 6 determines the file for today's content.
The PHP code for determining the modulo of 3252024 and 14 is 3252024%14
(in PHP code, the modulo operation uses the symbol %
).
Publishing the Daily Content
To insert the daily content into a web page, use this code:
<div id="abc-123-itisme"> <script type="text/javascript" src="https://example.com/dailycontent/syndicate.php?id=abc-123-itisme" async> </script> </div>
In the above code, note the URL to syndicate.php
is colored red. Change that URL to the URL of syndicate.php
that you uploaded to your server.
Also, notice the id value abc-123-itisme
in 2 places. It is the id
value of the div and it is part of the data appended to the syndicate.php
URL. The abc-123-itisme
must be identical in both places. If you change one, change them both.
When you have it working, that is the code you give to everybody who wants to publish your daily content. If you prefer, you can give them the above as one line:
<div id="abc-123-itisme"><script type="text/javascript" src="https://example.com/dailycontent/syndicate.php?id=abc-123-itisme" async></script></div>
In essence, what you have now is a way to syndicate your daily content on any web page at any websites.
You provide the daily content. The software does the rest, including choosing which to publish on any day.
(This article first appeared in Possibilities newsletter.)
Will Bontrager