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:
-
Copy the source code for the PHP script (see further below).
-
Save the source code to a file named Grabnpublish.php (or any other file name ending with ".php").
-
Upload the file to your server.
-
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.
-
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.
-
Replace url-to-script with the URL of the PHP script.
-
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