Skip to content

Instantly share code, notes, and snippets.

@franz-josef-kaiser
Last active July 29, 2022 10:01
Show Gist options
  • Save franz-josef-kaiser/4063197 to your computer and use it in GitHub Desktop.
Save franz-josef-kaiser/4063197 to your computer and use it in GitHub Desktop.
WordPress debug - Error Reporting Level: Paranoia
<?php
define( 'DS', DIRECTORY_SEPARATOR );
# ==================================
# PHP errors & log
error_reporting(
E_ALL | E_STRICT | E_CORE_ERROR | E_CORE_WARNING
| E_COMPILE_ERROR | E_ERROR | E_WARNING | E_PARSE
| E_USER_ERROR | E_USER_WARNING | E_RECOVERABLE_ERROR
);
# OR: shorter and all together
# error_reporting( -1 );
@ini_set( 'display_errors', 1 );
@ini_set( 'log_errors', 1 );
# ==================================
# DEBUG
define( 'WP_DEBUG', true );
// file: ~/WP_CONTENT_DIR/debug.log
define( 'WP_DEBUG_LOG', true );
define( 'WP_DEBUG_DISPLAY', true );
define( 'SAVEQUERIES', true );
# DEBUG: MU
define( 'DIEONDBERROR', true );
define( 'ERRORLOGFILE', WP_CONTENT_DIR.DS.'logs'.DS.'mu_error.log' );
# PHP Error log location
@ini_set( 'error_log', WP_CONTENT_DIR.DS.'logs'.DS.'php_error.log' );

Paranoia helps you making new friends

There are some things that you'll encounter when you start coding with PHP and have your first "Y U NO WÖRK?!"-moments. The reasons are in most cases pretty simple. In one sentence:

You didn't check. Period.

While PHP allows you to be lazy, it punishes the most minor mistake and burns you in hell. While this might be ok for you if you're coding something for yourself and for fun, it is not ok if later developers have to interact, build upon and extend your code. The reason is simple: If someone takes your piece of software and uses it as 3rd party code in his project, then he needs to trigger his own errors, warnings and notices. And those are hard to find if they're hidden between other error messages.

Write sustainable code. Keep your (code) environment clean.

It's as easy as that. Write code. Check values. Make new friends.

Just because dad didn't forbid it, doesn't mean he did allowed it

The most common error someone stumbles upon is a notice about an Undefined index. That's annoying and can be easily avoided. Just do strict checks, decide early what the content of your variable will be and stick with it.

Let's set up some vars:

// All our output depends on the value of $baz
$baz = absint( 3 );
// By default, $foo is an integer of the value 1 ...
$foo = absint( 1 );
// ... but if $baz is larger than (int) 2, $foo is true
2 < $baz AND $foo = true;

And then do some checks that rely on those vars:

// Now let us not output anything.
// As $baz is greater than two, foo was set to true
// The strict typecheck is against "1" and the type of integer
if ( 1 === $foo )
{
	echo 'This will not trigger, as $baz is larger than 2.';
}
// ... and now let's lie to us, because we forgot that 1 isn't always an integer.
if ( $foo == 1 )
{
	echo 'Foo is one! And this is a lie!';
}

Imagine you've written a large theme or plugin with multiple files and things are dependent on $foo in different places. I guess you already know it: You'll have a hard time finding that bug and in the meanwhile things just brake.

Some rules to stick with:

  • Check if stuff isset() or empty().
  • Use "Yoda Conditions" - assigning vars in the wrong place is not funny.
  • Do strict checks like !== and ===. It's also faster.
  • Decide what your vars type is. Then stick with it.
  • Do the exact same for your functions/methods return values.
  • Typecasting is in 99% of all cases not necessary. Save that for validation/sanitize stuff. Just see them as "last resort"-extension of all those funky esc_*() functions WordPress has to offer.
  • Test your code. Then test it again. With full debug environment turned on.

Develop in a local MU install. It doesn't cost a time, but you'll catch many more cases than you'd usually do. And make sure you got a plain vanilla install running that has only the default plugins and themes running and only test posts. You can care about everything else later on.

The paranoia error level

In this gist you can find part of my local config file, that shows you what I'm talking about when I say "Error Reporting Level: Paranoia". If you can write solid code that doesn't throw notices with those lines in your wp-config.php, then you're prepared for Zombieland.

A last note: If someone offers you a fix/patch/pull request for your plugin or theme, then fucking take it. Say thank you. Spread the word that someone helped you all social media channels you've access to. Buy him a beer.

Happy Coding!

Author links: Kaiser on G+, on Twitter & GitHub.

Important: In all above written lines, you'll find male references. That's just for the sake of readability. There're some women out there who write hell of a code and I deeply respect them. If you're one of them: Feel yourself included.

@KingGeneral
Copy link

KingGeneral commented Nov 6, 2018

Sometimes problem can come from your server side. so you can check :

<?php phpinfo(); ?>

and see if the settings is good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment