Open maheraldous opened 4 years ago
This sounds wrong. What changes have you done to your functions.php?
@mikaykun I pasted the code which is in the README.md but I got errors so I did some search and found that code I wrote to you and it worked for me. There is no need to update the README.md but put it in case someone like me needed.
Can you provide a example code so that we can fixed this issue or create a better solution for others and add it to the Readme. That would be nice.
I think @maheraldous is right. With
if ( ! file_exists( get_template_directory() . '/class-wp-bootstrap-navwalker.php' ) ) {
// File does not exist... return an error.
return new WP_Error( 'class-wp-bootstrap-navwalker-missing', __( 'It appears the class-wp-bootstrap-navwalker.php file may be missing.', 'wp-bootstrap-navwalker' ) );
} else {
// File exists... require it.
require_once get_template_directory() . '/class-wp-bootstrap-navwalker.php';
}
you simply return an instance of the WP_Error class holding the error code class-wp-bootstrap-navwalker-missing
and the error message It appears the class-wp-bootstrap-navwalker.php file may be missing.
. But the object is not processed in anyway, ie won't show up anywhere.
@maheraldous solution is a complicated way to echo the error message on every page (admin and non-admin pages for logged in and logged out users).
My proposal is
$file = get_template_directory() . '/class-wp-bootstap-navwalker.php';
if ( ! file_exists( $file ) ) {
// Files does not exist, add admin notice.
add_action( 'admin_notices', function() {
$message = sprintf(
/* translators: path to file */
__( 'The file class-wp-bootstap-navwalker.php cannot be found at %s.', 'wp-bootstrap-navwalker' ),
get_template_directory()
);
printf( '<div class="notice notice-error"><p>%s</p></div>', esc_html( $message ) );
} );
} else {
// File exists, require it.
require_once $file;
}
which adds an admin notice on admin pages if the file does not exist.
Your code didn't work for me but it worked somehow when I added the code in a funcation and then used the if statement to process the error.