Hi all,
Theme reviewer found some PHP notices in my theme that I couldn’t manage to reproduce in my environment:
http://envato.d.pr/G0IIpc
- WP_DEBUG is set to true
- PHP display_errors set to On
- error_reporting set to E_ALL
- Checked with all PHP versions from 5.3 to 7.0 (on live cPanel server and also on local MAMP server)
- Checked with plain WP installation and also theme-unit-test-data.xml imported
- Checked theme with Theme Check plugin (with and without additional ThemeForest-check plugin)
Still I can’t see the notices. I think these are fairly easy to fix, but I simply can’t reproduce the notices. Could you please give me more info about environment in which you’re reviewing themes?
Thank you,
Milos
This is pretty much an issue non-experimented PHP devs face, they call properties or methods on objects without actually checking that the object is actually an object and not a null value.
Let me give you an example:
$row = $database->get_row('whatever query from here');
echo $row->column_name;
But what if $row
is empty? Then you get errors like you got above, so the right thing is to always make sure that the variables you’re working with are actually what you think they are.
In my case, I have this code:
$nav_menu = wp_get_nav_menu_object( $menu_location );
$menu_name = $nav_menu->slug;
Since return of wp_get_nav_menu_object can be either an object or false, I hope this is how to get rid of these notices:
$nav_menu = wp_get_nav_menu_object( $menu_location );
if ( $nav_menu !== false ) {
$menu_name = $nav_menu->slug;
}
You can generalise this by simply checking with empty()
: if ( ! empty( $nav_menu ) ) { ...
.
null
, false
, 0
, "0"
, array()
, ""
will always be empty()
.