• As part of what I am doing for a children’s charity in India I am setting up a small document management system. From my php plugin I can create folders and upload documents OK. I am struggling to download documents. To illustrate my problem I have set up a small test page which tries to download a file.

    You can access the page at: https://www.testdb.org/?page_id=6992

    As you will see when I run the page I get a warning for each call of header().

    The warning is:
    Warning: Cannot modify header information – headers already sent by (output started at /home/healit/testdb.org/wp-includes/class.wp-styles.php:225) in /home/healit/testdb.org/wp-content/plugins/healdb/include/health_camp.php on line 6

    The problem I have is since it is saying output has already been generated by a system file (class.wp-styles.php:225) I can’t use header calls.

    What is the solution to this?

    The test code is just trying to download a file from the server. For test purposes I have hard wired the file path into the code.

    The test code is listed below:

    Many Thanks for any help you can give,
    Colin Charlton

    <?php
    
    function health_camp()
    {
       $file_name = "documents/uk/board_meetings/2019-11-29/Technical Report";
          header('Content-Description: File Transfer');
          header('Content-Type: application/octet-stream');
          header('Content-Disposition: attachment; filename="'.basename($file_name).'"');
          header('Expires: 0');
          header('Cache-Control: must-revalidate');
          header('Pragma: public');
          header('Content-Length: ' . filesize($file_name));
          readfile($file_name);
    }

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Dion

    (@diondesigns)

    You are calling your function much too late in the WordPress load process. It must be called before any headers have been sent, and I believe the latest hook available for this is wp_headers. That means you must use the $wp_query instance to check the ID of the page because theme files are not loaded until after headers have been sent.

    If I were writing this, I would parse the URL manually and output the file at the time the plugin is loaded. This would eliminate a significant amount of overhead in the WP loading process. (Actually I’d first try to use a standalone script, then fall back to the above if a standalone script wasn’t possible.)

    FYI, make sure the last line in your function is exit;. Otherwise control will pass back to WordPress, which is not a good thing since you’ve already send output to the browser. 🙂

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    First thing to do is to enable debugging.

    Debugging in WordPress

    You have to make it so your code is executed before any of the theme code is executed. The theme does the output.
    Since you can only output headers once, you have to make sure yours is first.
    Look at the list of actions and their order to see where your code should be hooked:
    https://codex.wordpress.org/Plugin_API/Action_Reference

    Warning: Cannot modify header information – headers already sent by (output started at /home/healit/testdb.org/wp-includes/class.wp-styles.php:225) in /home/healit/testdb.org/wp-content/plugins/healdb/include/health_camp.php on line 6

    the healdb plugin calls ( /home/healit/testdb.org/wp-content/plugins/healdb/include/health_camp.php on line 6 ) wp-styles.php? this is a plugin that is not present on the wordpress repository you have to ask for support where such plugin exists.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘php header function call fails due to class.wp-style.php file’ is closed to new replies.