After my last post, I decided to write a WordPress plug-in that would upload text to a file and generate a script tag to use to reference that file in posts. This was just thrown together in an evening and most of the work was in learning how to get the plug-in to work as a main menu item in the dashboard and to do the AJAX call using jQuery. JQuery is used by WordPress and is available without any effort on my part. I wrote one or two posts a while ago that would make AJAX calls without jQuery, in case anyone doesn’t want to use jQuery.

The first file, of two needed, is the plug-in:

Note that I used my new plugin mechanism to upload the file above.

The plug-in file should be stored in the /wp-content/plugins/anypluginname/plugin.php file. Pick a directory name that will not conflict with any other plug-ins.

The plug-in file contains some code to add a main menu item to the WordPress dashboard. The main menu item can be seen in the following picture:


New Main Menu Item – WordPress Dashboard


Running the Plug-in

When the plug-in is used, by clicking on the menu item, a page is displayed that allows a file name and code, or other text, to be entered. Clicking on the “Create File” button makes the AJAX call to upload the data to a file. The proper HTML script tag to place in the blog post is then shown below the “Create File” button, as seen below:


Create Code File – Page After Submit

The other file that is used by this plug-in is the PHP file on the server that performs the AJAX action:

Again, the file above was created using this plug-in.

The AJAX PHP action file gets the data and the file name, and creates the uniquely named file with that data. The name is used as part of the unique name and also in the example script tag that gets shown back on the plug-in page.

This is some very simple stuff, but it does the trick and let’s me stick file contents, formatted as code, into any blog post. The data is in a file that can be viewed and downloaded.