I have a post that includes a relatively large table that gets updated frequently. Instead of using the (slower) web interface to update the post each time, I installed one of the ârun PHPâ plugins and put a short bit of PHP code in the table body that opens an external file on the server and puts that in the table in the post. This way, I can edit the file locally in a responsive text-editor as needed and the change is reflected immediately when the the page is served (and without requiring any changes to any of the WordPress tables).
This works pretty well and I am generally satisfied with it, but there is a small catch in that the path to the external text-file is relative to the store on the server (I used the media-library manager to store it, and because Iâm using the multi-site feature and PHP is server-side, the path is of the form "wp-content/blogs.dir/1/files/2011/02/foobar.txt"
). The paths to other files like images that client-side code like JavaScript uses is relative to the URL. This is obviously not ideal for several reasons.
So Iâve got two questions:
- Is there a better way to include external content in a post than using PHP?
- (well, 1b.) If not, is there a betterâie, more consistentâway of accessing the files? (That is, a way to have PHP use URL-relative paths instead of server-relative paths?)
Clarification:
In the post, I have something like this (code is off the top of my head, so ignore any errors):
â¦
<table>
<thead><th>foo</th><th>bar</th><thead>
<tbody>
<?php
$fn ="wp-content/blogs.dir/1/files/2011/02/foobar.txt";
$fh =fopen($fn, "r");
$data=fread($fh, filesize($fn)); //no clearstatcache; TXT file is not updated *that* often
print($data);
fclose($fh);
?>
<tbody>
</table>
â¦
Where the text file contains something like this:
<tr><td>Blah</td><td>123</td></tr>
<tr><td>Foobar</td><td>asd</td></tr>
<tr><td>Test</td><td>555</td></tr>
â¦
When the page is served, the PHP in the post is executed and pulls in the table rows from the external file, picking up any changes I made externally to it without having to go through WordPress to do so.
There is also some JavaScript before the table that deals sets some images according to a switch statementâthe images were imported with the media libraryâsomething like this:
case 'a': element.style.backgroundImage='url(/blogs/foobar/files/2011/02/A.png)'; break;
case 'b': element.style.backgroundImage='url(/blogs/foobar/files/2011/02/B.png)'; break;
â¦
1) Yes, there’s better ways…one would be to use a shortcode instead of storing php in your db. Write and register a shortcode callback that reads your file and returns the content and replace your
<?php ... ?>
section in the post with the shortcode. Also, you can read the file withfile_get_contents();
, saves you a few lines of code.2) You could write a function that simulates that, but the benefit would be too minimal to justify the effort.
BUT: Instead of hardcoding your file path into the shortcode, you can get its path via
get_attached_file($attachment_id)
.How often is this file updated? Why not pull the data and store it in a transient with a time limit. That way when it is served you aren’t processing anything and it’s also cached. ( and your url issue is also probably resolved).
You would typically also compare the stored transient value with most recent data and only update it if there is a change ( using
md5
ormd5_file
).http://codex.wordpress.org/Transients_API