Error when no 404.php defined in theme
-
I’m building a new theme for a client and noticed these messages pop up when I hit a bad URL (404 not found) and there’s no 404.php template specified for the theme:
Notice: Trying to get property ‘ID’ of non-object in /path/to/wp-content/plugins/acf-typography-field/includes/functions.php on line 84
Notice: Trying to get property ‘post_content’ of non-object in /path/to/wp-content/plugins/acf-typography-field/includes/functions.php on line 88
The obvious answer is: Add a 404.php template file since every theme really should have one anyways. But it’s not a REQUIRED file, and quite a few themes don’t have one. The theme I’m creating is a single page theme with ZERO other pages at all, so there’s no point in having the 404.php template.
I’m getting around this with a little code snippet to redirect any 404 back to the home/index.php page:
function fol_redirect_to_home_404() { if (is_404()) { $url = trailingslashit(get_home_url()); wp_redirect( $url ); exit; } } add_action('get_header', 'fol_redirect_to_home_404');
Even still, I can’t help but think there should be some sort of check within this plugin. Not sure the best way to go about it since some people may actually be using your plugin within 404 pages.
Perhaps check IF is_404() && 404.php exists within the theme, THEN do the code in functions.php starting on line 84. Otherwise, skip it.
It’s not so much an issue with your plugin, but it is something I ONLY encounter with your plugin when there’s no 404.php template. Perhaps it should be accounted for?
- The topic ‘Error when no 404.php defined in theme’ is closed to new replies.