Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
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!

Importing Content for WordPress Without Plugins

Some website owners who use WordPress, especially new site owners, assume a plugin is needed to insert content into their web pages if they don't themselves type or paste it in.

For some content, that's true. It would be content that is only available with a plugin.

For other content, lots and lots of other content, no plugin is required.

JavaScript can be used to import content generated with PHP scripts — which is the primary focus of this article.

Examples of content PHP scripts may generate that you may wish to publish on your post or pages are a quote-of-the-day script, ad or image rotation, news item, and special sale notices. Valid HTML content generated by any public PHP script can be published on your posts and pages.

Before the recent Guttenberg editing interface became standard in WordPress, it was a bit tricky to add JavaScript to posts and pages.

The code had to be all one line or WordPress helpfully inserted HTML br code that broke the JavaScript. (The pre-Guttenberg work-around was to use the "Text" tab instead of the "Visual". Yet, later editing in the "Visual" tab would break the JavaScript.)

With the latest WordPress editing interface:

  1. At the editing page, open a "Custom HTML" block:

    • One way is to tap the +circle icon in the block insert area and choose "Custom HTML".

    • Another way is to hover over the "[…] choose a block" phrase and tap the little "HTML" icon.

  2. In the Custom HTML block, you'll see a text box. Type or paste the custom content you want in the post or page.

    For implementing the technique in this article, you would paste the JavaScript that imports the content generated by a PHP script.

Publishing Content Generated With a PHP Script

I'll first describe how it works. Then present the source code and instructions.

How it works

You are interested in publishing the content that a certain PHP script outputs.

  • When the certain PHP script runs, it outputs HTML content. (We'll call this the source PHP script.) The content may include images and even JavaScript.

    This is the content you want to publish on your WordPress post or page.

    The source PHP script may be on your domain or somewhere else on the internet.

  • The content the source PHP scripts output is filtered through another PHP script. (We'll call this the filter PHP script.) The filter PHP script converts content into JavaScript code.

  • To publish the content from the source PHP script, you use a script tag. The script tag tells the filter PHP script the URL of the source PHP script.

    The filter PHP script gets content from the source PHP script, converts the content to JavaScript code, and sends the JavaScript code to the script tag.

Now that you see how it works, let's implement the functionality.

Source code and instructions

Below is the source code for the filter PHP script. It needs to be uploaded to your server. (No customization is required.)

Name the source code file as filter.php or whatever *.php file name suites your preference. When uploaded, make a note of its URL.

<?php
/* Text to JavaScript Filter
   Version 1.0, June 5, 2020
Will Bontrager Software LLC
https://www.willmaster.com/
*/
header('Content-type: text/javascript');
if( empty($_GET['js']) ) { EchoTextToJavaScript('Inappropriate access.'); }
if( preg_match('!\.\.!i',$_GET['js']) ) { EchoTextToJavaScript('No directory traversing allowed.'); }
EchoTextToJavaScript(file_get_contents($_GET['js']));
function EchoTextToJavaScript($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);
      $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);
      $js = preg_replace('/(vi)(deo)/i','$1\'+\'$2',$js);
      echo "document.writeln('$js');\n";
   }
   exit;
}
?>

Now, create a script tag to pull in the content of any public PHP script on the internet and publish it on your post or page. Here is an example:

<script 
   src="https://example.com/filter.php?js=https://willmaster.com/2/nonJSoutput"
   type="text/javascript">
</script>

There are two customizations to make to the above script tag. Then it will be ready to paste into your WordPress Custom HTML block text box.

  1. Replace https://example.com/filter.php with the URL of the above filter PHP script that was uploaded to your server.

  2. Replace https://willmaster.com/2/nonJSoutput with the URL of a source PHP script that outputs content you want to publish in your post or page. The URL needs to be the full http://… or https://… URL.

With the functionality in place, you can use the script tag in any posts or pages and publish the output of any publicly-available source PHP script.

(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