Suppress PHP warnings and notices in WordPress pages - php

When I try to load WordPress, I get a lot of Use of undefined constant ‘view’ - assumed '‘view’' type of warnings and notices in the browser. This causes the pages to fill up with these messages before it renders the actual page content expected.
I tried changing error_reporting = E_ALL to error_reporting = E_ALL & ~E_NOTICE & ~E_WARNING but the warnings and notices still show up.
After doing php --ini I located both the 7.0 and 7.1 ini files and updated the value in both and restarted both FPM services on my vagrant.
/etc/php/7.0/fpm/php.ini
/etc/php/7.1/fpm/php.ini
Why are these still showing up?

You can combine WordPress build-in constants and PHP's ini settings.
Place these lines in your wp-config.php
ini_set('log_errors','on');
ini_set('display_errors','off');
ini_set('error_reporting', E_ALL );
define('WP_DEBUG', false);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);
This way all notices, warnings and errors will not be shown on the front-end of your website, but errors are still accesable by a log file.

Looks like you copied the code from some source and pasted in your file, the inverted quote ` is used instead of ', simply change the single quote and it will be fixed.
change ‘view’ to 'view' and so on.

To show errors and hide notices (and as was asked, warnings) there is a hacky way in wp-config.php:
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_DISPLAY', true );
plus this at the end of the file after wp-settings.php is loaded:
/*hide notices and warnings*/
error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING );
I'm saying hacky only because it's after the line saying /* That's all, stop editing! Happy publishing. */ :)

Related

Why is the wordpress fatal error not being displayed?

Our site was migrated due to issues on our server providers end. It caused many errors and I fixed most of them. Now, for some pages, it just says there is an error. It display notices and a depreciated warning but I know this will not cause the entire page to stop working. Why is the fatal error not being displayed?
This is what I have tried.
define( 'WP_DEBUG_LOG', 'error.log' );
ini_set('display_errors','on');
ini_set('error_reporting', E_ALL );
define('WP_DEBUG', true);
define('WP_DEBUG_DISPLAY', true);
error_reporting(E_ALL);
Normally it would show a fatal error. I don't know why it isn't showing me the important error.
Please help!
First ensure that there is no place in your code where you set error_reporting to 0
e.g.
error_reporting(0);
as this will totally hide all error warnings and notices.
In php script, this three line of code will just turn it on
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
You should see the same messages in the PHP error log.
Other things that you will need to check
1.) PHP.INI Files or your Apache configurations
You can move to your php.ini files and ensure that the following parameters below are commented out and set on.
By commented out, I mean removing a semicolon**(;)** at back of it
Eg.
uncomented = ;display_errors=On
commented = display_errors=On // semicolon removed
display_errors=On
display_startup_errors=On
track_errors = On
html_errors=On
shutdown and restart apache for it to take effect.
2.) .HTACCESS Files
You can also check .htaccess files to see if any of these warning parameter flags are set to 0 which means off and set them to 1 which means On.
Remember to turn off all these error warnings in production.

PHP built in webserver error reporting

I'm using the built in PHP server and I'd like to suppress the strict warnings. In my php.ini file i have:
error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
...but it is still printing strict and deprecated notices. I verified that I was editing the correct ini file by checking a page with phpinfo().
In the built in webserver documentation, there is no mention of special error reporting rules.
I have also tried this:
error_reporting(E_ALL ^ E_STRICT ^ E_DEPRECATED);
ini_set("display_errors", "off");
print "changed stuff";
"changed stuff" is printed, along with strict and deprecated notices.
What do I need to do to suppress these errors in the PHP built in webserver? (Can this be done?)
Try changing like this [Reports all errors except STRICT ]
Through Code
<?php
error_reporting(E_ALL ^ E_STRICT);
Through PHP.ini
error_reporting = E_ALL ^ E_STRICT
I searched through my code (it's a very old codebase) and found several instances where the error reporting was manually set. Once i removed those it worked.
Check your files for error_reporting calls

disabling deprecated errors

I want to enable deprecated errors globally but disable them for a specific piece of third-party code, which I don't have the time to fix.
So, I have this in php.ini:
error_reporting = E_ALL & ~E_NOTICE | E_DEPRECATED
and this right before the line where I want to disable the warnings
error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);
However, it does not work, I'm still getting the warnings for that particular line. If I disable them globally in php.ini it works. I'm using PHP 5.3.10. Any ideas what might be wrong?
Figured it out. The third party code has custom error handler and apparently it's overriding anything you set with error_reporting(). When I commented out the set_error_handler() line, error_reporting() took effect.
Add the below error reporting line in your php.ini:
error_reporting(E_ALL & ~E_WARNING & ~E_NOTICE & ~E_DEPRECATED);
Then restart your server and check it.

wamp - notice undefined offset

How to turn off this error in wamp:
notice undefined offset
I'd like to turn of just this error, but not all errors.
There are two issues at work here. One is what errors PHP reports, and the second is whether or not it displays those errors on the page (as opposed to the apache error log). If you'd like to turn off just NOTICES:
<?php
error_reporting(E_ALL & ~E_NOTICE);
?>
If you'd like to report the notices to your error log but not display them to the user, do this:
<?php
ini_set('display_errors','off');
?>
Note that turning off display errors will stop displaying ALL errors to the end user, and you'll need to look at the error log, usually located in /var/log/httpd/error_log to see any errors while testing.
Have a look at error_reporting().
You could e.g. set the error reporting to
error_reporting(E_ERROR | E_WARNING | E_PARSE)
But better would be to actually check what is the cause of the Notice and fix it. Then you are on the save side.
E_NOTICE
Run-time notices. Indicate that the script encountered something that could indicate an error, but could also happen in the normal course of running a script.
(If you can't fix the code...) You can exclude notices by setting an reporting level x & ~E_NOTICE, e.g.
<?php error_reporting( error_reporting() & ~E_NOTICE );
or in your php.ini (or similar)
error_reporting=E_ALL & ~E_NOTICE
php.ini => error_reporting = E_ALL & ~E_NOTICE
error->notice undefined offset
main thing is remove the error on your script.Programmer always wish to design program which is error free instead of error hiding.
The array vales are not set, so when PHP is trying to access the value of those array keys it encounters an undefined offset.
$new_array = array('1','2','3');//If I have an array
//We can now access:
$new_array[0];
$new_Array[1];
$new_array[2];
//If we try and access
$new_Array[3];
we will get the same error-->error->notice undefined offset
error_reporting(E_ALL);
ini_set('display_errors', 'On');
ini_set('html_errors', 'Off');
:D
Go to This direction on windows OS C:\wamp\bin\apache\Apache2.4.4\bin
Then open php.ini file by any editor recommended [sublime] in 514 line just paste this two lines.
;error_reporting = E_ALL error_reporting = E_ALL & ~E_NOTICE

PHP 5 disable strict standards error

I need to setup my PHP script at the top to disable error reporting for strict standards.
Can anybody help ?
Do you want to disable error reporting, or just prevent the user from seeing it? It’s usually a good idea to log errors, even on a production site.
# in your PHP code:
ini_set('display_errors', '0'); # don't show any errors...
error_reporting(E_ALL | E_STRICT); # ...but do log them
They will be logged to your standard system log, or use the error_log directive to specify exactly where you want errors to go.
For no errors.
error_reporting(0);
or for just not strict
error_reporting(E_ALL ^ E_STRICT);
and if you ever want to display all errors again, use
error_reporting(-1);
All above solutions are correct. But, when we are talking about a normal PHP application, they have to included in every page, that it requires. A way to solve this, is through .htaccess at root folder.
Just to hide the errors. [Put one of the followling lines in the file]
php_flag display_errors off
Or
php_value display_errors 0
Next, to set the error reporting
php_value error_reporting 30719
If you are wondering how the value 30719 came, E_ALL (32767), E_STRICT (2048) are actually constant that hold numeric value and (32767 - 2048 = 30719)
The default value of error_reporting flag is E_ALL & ~E_NOTICE if not set in php.ini.
But in some installation (particularly installations targeting development environments) has E_ALL | E_STRICT set as value of this flag (this is the recommended value during development). In some cases, specially when you'll want to run some open source projects, that was developed prior to PHP 5.3 era and not yet updated with best practices defined by PHP 5.3, in your development environment, you'll probably run into getting some messages like you are getting. The best way to cope up on this situation, is to set only E_ALL as the value of error_reporting flag, either in php.ini or in code (probably in a front-controller like index.php in web-root as follows:
if(defined('E_STRICT')){
error_reporting(E_ALL);
}
In php.ini set :
error_reporting = E_ALL & ~E_NOTICE & ~E_STRICT
WordPress
If you work in the wordpress environment, Wordpress sets the error level in file wp-includes/load.php in function wp_debug_mode(). So you have to change the level AFTER this function has been called ( in a file not checked into git so that's development only ), or either modify directly the error_reporting() call
I didn't see an answer that's clean and suitable for production-ready software, so here it goes:
/*
* Get current error_reporting value,
* so that we don't lose preferences set in php.ini and .htaccess
* and accidently reenable message types disabled in those.
*
* If you want to disable e.g. E_STRICT on a global level,
* use php.ini (or .htaccess for folder-level)
*/
$old_error_reporting = error_reporting();
/*
* Disable E_STRICT on top of current error_reporting.
*
* Note: do NOT use ^ for disabling error message types,
* as ^ will re-ENABLE the message type if it happens to be disabled already!
*/
error_reporting($old_error_reporting & ~E_STRICT);
// code that should not emit E_STRICT messages goes here
/*
* Optional, depending on if/what code comes after.
* Restore old settings.
*/
error_reporting($old_error_reporting);

Categories