WordPress.org

Ready to get started?Download WordPress

Forums

post.php comes back as blank page (4 posts)

  1. TIves
    Member
    Posted 1 year ago #

    This is specific to post.php where post.php returns blank after clicking "Publish".

    I just spent the entire day trying to solve this and I though I would put this out there for anyone else who may struggle with this.

    First I have to say that I cannot believe how sensitive WordPress can be to white space in PHP files.

    Symptom: You hit publish and when post.php returns it is totally blank and no variables are present in the URL (GET).

    So, as far as I can tell, for any plug-in that you build to create meta-boxes and/or run logic on the admin editor page (in my case edit-form-advanced.php), you have to be EXTREMELY careful about interjecting white space before, after on in the middle of your php code.

    Apparently, one way to help with this is to add:

    php_flag output_buffering on

    to the .htaccess file. However, if you're like me your hosting provider does not provide you access to .htaccess.

    Here are some examples....

    // COMMENTS, HTML OR WHITE SPACE HERE WILL KILL YOU (top of file)
    <?php
    
        //here is some php code...
    
    ?>
    // COMMENTS, HTML OR WHITE SPACE HERE WILL KILL YOU (middle of file)
    <?php
    
        //here is more php code...
    
    ?>
    // COMMENTS, HTML OR WHITE SPACE HERE WILL KILL YOU (end of file)

    Calling a script: (Below) Notice the placement of the <script> tag. I found that if I placed anywhere outside of a function or anywhere outside of the php block (i.e. line #1 or last line of code) post.php would blow-up after hitting Publish. You must break the php mid-function, add the script, and then restart the php block with no white space.

    <?php
    
       function my_function(){
          //some php code...
    ?>
       <script type="text/javascript" src="my_script_file.js"></script>
    <?php
       }
    
    ?>

    This will work too...

    <?php
    
       function my_function(){
          //some php code...
          echo '<script....></script>';
       }
    
    ?>

    OK, I hope this helps some other poor slob like me. I apologize if this is too rudimentary, but I am fairly new to PHP and WordPress. However, I have never encountered anything like this in another language.

    All the best!

    Trip

  2. catacaustic
    Member
    Posted 1 year ago #

    This is something that's more specific with PHP and the way that it interacts with the web server. Anything outside of <?php ?> tags is considered to be HTML code. There's no exceptions to that rule. Any time you want to use comments in a PHP file put them inside the PHP tags, that way you can have anything that you want, and there's no issues with white space, carriage returns, tabs or anything else. Any time you want to add HTML coce you need to close and re-open the PHP tags, or use an echo statement like you've said above. The only time that changes is that you don't have to have the last closing ?> tag at the end of a file (assuming that it ends in a PHP block and not a HTML block).

    Which brings me to another point here. Please tell me that you're not. That's a very bad idea. If you're doing this in your own files, then it's alright, but I've seen a lot of people lere that do try to make modifications that they really shouldn't. As soon as the next update is rolled out your changes will be over-written with the new file and you will loose everything that you've done. You could always jsut never update, but then you're leaving yourself open to an vunerabilities that are discovered so I'd hope that's not an option. I hope that

  3. TIves
    Member
    Posted 1 year ago #

    Thanks for the clarifications catacaustic!

    As you said, white space in php is a sticky issue in general. However, in the case of working with post.php, I found it to be even more nit-picky. But, as I said, I'm a newbie too.

    As for your question, if I understood it correctly, the answer is no, I am not modifying native WordPress files (although I have done that before :-)). This is 100% plug-in and self contained. It just happens to be loaded along with the post.php file via add_action ('add_meta_boxes', ...).

    All the best!

    Trip

  4. catacaustic
    Member
    Posted 1 year ago #

    It's not post.php that's the "problem" with this. Almost al lof the WP core files will have the same issue depending on where the processing takes place. It's the way that WordPress (and PHP) works. The HTML content of the page isn't processed until a fair while after the system is loaded, initialised and a bit of processing happens, so when there's some errant white space that's output it will always break any calls to a function like header() to do a redirection. There's also other functions that will have issues with it too but in general, that's the main one that causes 99% of the problems. It's one of those things that pretty much ever PHP developer has had to deal with from time to time, so it's just experience with the language that makes the difference, the same as learning anything new. :)

Topic Closed

This topic has been closed to new replies.

About this Topic