• Resolved raphaelfabeni

    (@raphaelfabeni)


    In some PHP there are some elements that have many attributes. For better code legibility, each attribute is listed in a new line. Something like this:

    <button
      type="submit"
      class="button"
      id="button-x"
      data-id="x"
      data-attr-abc="test"
    >
    text
    </button>

    But for some reason, Autoptimize is not minifying it and keeps the line breaks. Is it expected because is a PHP file or Autoptimize should be able to handle this?

Viewing 15 replies - 1 through 15 (of 17 total)
  • Plugin Author Optimizing Matters

    (@optimizingmatters)

    AO (or rather the HTML minifier used by AO) indeed keeps the linebreaks raphaelfabeni.

    Thread Starter raphaelfabeni

    (@raphaelfabeni)

    I see… that’s sad!
    Anyways, Thank you! πŸ™‚
    And keep the good work!

    Plugin Author Optimizing Matters

    (@optimizingmatters)

    Well, you can hook into AO’s API (the autoptimize_html_after_minify filter) to remove those linebreaks. I think I have something like that floating around on one of my servers even πŸ˜‰

    Thread Starter raphaelfabeni

    (@raphaelfabeni)

    Really? Interesting! πŸ™‚
    I will take a look at it!

    Plugin Author Optimizing Matters

    (@optimizingmatters)

    let me know if you need example code πŸ™‚

    Thread Starter raphaelfabeni

    (@raphaelfabeni)

    I was doing it right now! If you could send it to me here I’d love to compare!
    Thanks πŸ™‚

    Plugin Author Optimizing Matters

    (@optimizingmatters)

    hmm, couldn’t find the code I had in mind, but something like the following would likely do the trick;

    
    add_filter( 'autoptimize_html_after_minify', 'remove_newlines');
    function remove_newlines( $html_in ) {
        return str_replace( array('\r', '\n'), ' ', $html_in );
    }
    Thread Starter raphaelfabeni

    (@raphaelfabeni)

    I did almost the same!

    
    add_filter( 'autoptimize_html_after_minify',  function($content) {
      $content = str_replace(array("\r", "\n"), '', $content);
        return $content;
    });
    
    Plugin Author Optimizing Matters

    (@optimizingmatters)

    good job πŸ™‚

    Thread Starter raphaelfabeni

    (@raphaelfabeni)

    Thanks again for all the support!
    πŸ™‚

    Plugin Author Optimizing Matters

    (@optimizingmatters)

    you’re welcome, feel free to leave a review of the plugin and support here! πŸ™‚

    Thread Starter raphaelfabeni

    (@raphaelfabeni)

    Sure!

    Thread Starter raphaelfabeni

    (@raphaelfabeni)

    Hey! πŸ™‚ Just getting back to this: I used the code snippet you sent me.. but it seems is not working. The one that I wrote I got an error (Unexpected end of input). Probably because I have some JS inline in the page.

    Plugin Author Optimizing Matters

    (@optimizingmatters)

    ah, my code had single-quotes around the elements in the array, but those should be double-quotes. but afraid the same problem will occur (JS linebreaks), so some more advanced technique will have to be used to exclude script-blocks, something like this;

    
    add_filter( 'autoptimize_html_after_minify', 'remove_newlines');
    function remove_newlines( $html_in ) {
    	$html_in = autoptimizeBase::replace_contents_with_marker_if_exists(
    		'SCRIPT',
    		'<script',
    		'#<(?:no)?script.*?<\/(?:no)?script>#is',
    		$html_in
        );
        $html_in = str_replace( array("\r", "\n"), ' ', $html_in );
    	$html_in = autoptimizeBase::restore_marked_content( 'SCRIPT', $html_in );
    	return $html_in;
    }
    Thread Starter raphaelfabeni

    (@raphaelfabeni)

    Awesome! I will try it! Thanks for the quick reply!

Viewing 15 replies - 1 through 15 (of 17 total)

The topic ‘Not minifying new lines’ is closed to new replies.