Modifying a php include with ob_start not working when extra edits are made

47 Views Asked by At

I'm trying to modify a series of headers via PHP Includes and am running into issues when I modify the buffer more than once.

I'm essentially trying to make it super easy to edit the included js and css files and if we look at the following code, this is successfully replacing the ?v=x with a query string of a version number.

<?php
    function callback($buffer) {
      return (str_replace("?v=x", "?v=1.3", $buffer));
    }
    ob_start("callback");
    include 'includes/header-tags.php';
    ob_end_flush();
?>

The page is outputting the correct links with the version of 1.3 applied to them.

I tried to modify it so I could use these same headers in subfolders with the following code, as you can see I've attempted a directory fix for the css/js folders by replacing every instance of ="assets/ with ="../assets/ which would fix the path.

<?php
    function callback($buffer) {
        $buffer = str_replace("?v=x", "?v=1.3", $buffer);
            
        // Directory Fix for Subfolders
        $subfolder_array = array("extra-pages", "test-pages", "new-pages", "custom");
        if (in_array(trim(basename(dirname(__FILE__))), $subfolder_array)) {    
            $buffer = str_replace('="assets/', '="../assets/', $buffer);
        }
      return $buffer;
    }
    ob_start("callback");
    include 'includes/header-tags.php';
    ob_end_flush();
?>

However it's not outputting any css/js files, the include is essentially returning nothing...

I'm sure it's something silly that I've missed or not understood, I'd appreciate some help.

Thanks

0

There are 0 best solutions below