1

In order to optimize my website I want to replace several things in a whole HTML. I was able to replace things in the content with the following function:

function replace_text($text) {
    $text = str_replace('look-for-this-string', 'replace-with-this-string', $text);
    return $text;
}
add_filter('the_content', 'replace_text');

But, for example to minify the generated html I want to delete the line breaks. Is there a way of doing so in functions.php? How can I archive this?

matiit
  • 7,969
  • 5
  • 41
  • 65
jherran
  • 3,337
  • 8
  • 37
  • 54
  • If your method works, so why not to add `$text = str_replace(array("\r", "\n"), '', $text);` before `return $text`? Our create another filter for minifying and call it next. – Gino Pane Jun 15 '16 at 08:23
  • @GinoPane I want it to work in whole html, not just the content. – jherran Jun 15 '16 at 08:24
  • 1
    http://stackoverflow.com/questions/6225351/how-to-minify-php-page-html-output <--you can use a modified version of this – SML Jun 15 '16 at 08:28

1 Answers1

0

You can either use or check how it's done in existing plugins, can't you? For example:

WP Super Minify

This will give you not only a solution, but also you will have a nice opportunity to check how people are writing plugins. And who knows, maybe you could even contribute to this plugin?

matiit
  • 7,969
  • 5
  • 41
  • 65