Software, your way.
burger menu icon
WillMaster

WillMaster > LibraryWordPress Widgets & Other WP 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!

Import Any Content Into WordPress

Content from the internet can be inserted into WordPress posts and pages, easily — any public content from sites that don't have blocks in place.

There's a plugin that can do it, and I'll mention it in a moment. But if you don't want to use a plugin just to import content into a few pages, the PHP script accompanying this article provides the functionality you want.

The PHP script is launched only when the post or page with the imported content is loaded into a browser. Otherwise, no CPU is required to have the functionality available.

Before we go on, let me mention the plugin. It's my very first WordPress plugin, designed for exactly what this article describes, importing content into posts and pages. The Local Syndication plugin has never had any issues reported. In other words, it works a treat. The plugin can be obtained at WordPress.org or at Willmaster.com

Some people, however, prefer not to use a plugin if it can be avoided. Perhaps they have so many plugins already that their page delivery is slowing down.

If you're in that group, the PHP script further below is what you need. The script requires no customization; simply upload to your server and make a note of its URL.

Importing content into a post or page is with a one-line JavaScript tag.

The JavaScript tag tells the PHP script what to import and the PHP script delivers it. (It works with non-WordPress pages, too; any web page where JavaScript can be used.)

This example is imported with the one line of JavaScript code.

Here's the one-line JavaScript for the above example. Paste it into your own test page to see it work.

<script type="text/javascript" src="https://www.willmaster.com/library/example/jsimport/Grabnpublish.php?URL=https://www.willmaster.com/library/example/jsimport/testing.php"></script>

Implementing the import-content system

For the one-line JavaScript to work, a PHP script needs to be uploaded to your server:

  1. Copy the source code for the PHP script (see further below).

  2. Save the source code to a file named Grabnpublish.php (or any other file name ending with ".php").

  3. Upload the file to your server.

  4. Make a note of the file's URL (you'll need the URL for the JavaScript).

Here's the source code of the PHP script:

<?php
/* 
   Grab URL and Publish as JavaScript
   Version 1.0
   July 13, 2014

   Will Bontrager Software LLC
   https://www.willmaster.com/
   Copyright 2014 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.
   Will Bontrager Software LLC grants 
   you a royalty free license to use or 
   modify this software provided this 
   notice appears on all copies. 
*/
if( empty($_GET['URL']) ) { exit; }
header('Content-type: text/javascript');
if( ! ($text = file_get_contents($_GET['URL'])) ) { exit; }
$text = str_replace("\r",'',$text);
foreach( explode("\n",str_replace("\r",'',$text)) 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);
   echo "document.writeln('$js');\n";
}
exit;
?>

Now that the PHP script is on the server and you know its URL, here's how to construct the one-line JavaScript.

  1. Start with this:

    <script type="text/javascript" src="url-to-script?URL=url-to-content"></script>
    

    The two customizations are marked with blue text and with orange text.

  2. Replace url-to-script with the URL of the PHP script.

  3. Replace url-to-content with the URL of the content to import into your web page.

Paste the one line of JavaScript where you want the imported content to be published and you're good to go.

WordPress and non-WordPress pages

The system described in this article works with both WordPress posts and pages and non-WordPress pages. It will work with any web page that can use JavaScript.

For WordPress, there's the Local Syndication plugin option mentioned further above. But if you prefer not to install yet another plugin, this system works a treat.

(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