-3

Viewing the source code of my WordPress-powered website looks something like this:


enter image description here

Is there a way to clean that up a bit so the code looks more like this:


enter image description here

Where everything is left aligned and empty lines are stripped out. I know I could modify my theme's php files and align it left myself, but I'd rather it be done when the page is rendered.

I've run across a few code snippets and plugins that will minify the page completely, but those take it too far.

I just want my source code to look cleaner.

Rich
  • 1,136
  • 3
  • 16
  • 36
  • Every decent IDE has a function like the one you are searching for. Ie, Netbeans can do that with the shortcut alt+shift+F. What IDE are you using? – Masiorama Feb 11 '15 at 15:42
  • http://stackoverflow.com/questions/7107140/beautify-html-output – Lost F. Feb 11 '15 at 15:43
  • @Masiorama I'm using GitHub's Atom.io. Do they allow you to keep alignment when editing a file, but clean it up on on save? – Rich Feb 11 '15 at 15:44
  • then check this out: http://stackoverflow.com/questions/22611377/auto-indent-code-in-atom-editor; about the on-save feature, I don't know. – Masiorama Feb 11 '15 at 15:46
  • Lots of plugins will add code to the head section which you won't be able to align like this. Furthermore there's no need to. It's nice to have the actual files you're working with indented in a neat and consistent manner but the output is for the browser. Instead look into compressing the entire thing. – Nathan Dawson Feb 11 '15 at 15:48
  • Hi, I use [W3 Total Cache](https://wordpress.org/plugins/w3-total-cache/) to compress all files for production sites. Note that this will not align your code left buy it will completely strip out all line breaks, indents, spaces, etc... – Siegfried Grimbeek Feb 11 '15 at 16:54

1 Answers1

0

I found a simple solution using regular expression:

ob_start( function ( $output ) {
    return preg_replace( '/(?:(?:\r\n|\r|\n)\s*){1}/s', "\n", $output );
});

Pasting this at the top of Wordpress' index.php file does the job.

Rich
  • 1,136
  • 3
  • 16
  • 36