Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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:

  1. Is there a better way to include external content in a post than using PHP?
  2. (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;
…
share|improve this question
    
What kind of data are you storing? How is it being displayed; in a page's body? Is the site on the web, and if so what's the URL to the webpage that stores it? Where are you using "run PHP"; in a widget? Understanding your use-case can help us recommend the best option. –  MikeSchinkel Feb 26 '11 at 22:53
    
I’ve updated above to address your questions. –  Synetech Feb 27 '11 at 1:06
    
I'm assuming the php is in the template not the post right? –  Chris_O Apr 28 '11 at 10:59
    
@Chris_O, what template? The theme? No, it’s in the post. –  Synetech Apr 28 '11 at 21:26
    
Instead of opening the file, reading it, etc.. Just use the readfile function. It reads a file and spits it straight to the output. One line of code. See php.net/manual/en/function.readfile.php –  Otto Feb 11 '12 at 18:41
add comment

2 Answers

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 or md5_file).

http://codex.wordpress.org/Transients_API

share|improve this answer
add comment

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 with file_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).

share|improve this answer
    
I am not familiar with transients; I’ll look into it. –  Synetech May 28 '11 at 16:19
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.