Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update README.md #495

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

maheraldous
Copy link

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.

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.
@mikaykun
Copy link

This sounds wrong. What changes have you done to your functions.php?

@maheraldous
Copy link
Author

@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.

@mikaykun
Copy link

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.

@IanDelMar
Copy link
Collaborator

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.

image

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

Successfully merging this pull request may close these issues.

3 participants