wp-bootstrap / wp-bootstrap-navwalker

A custom WordPress nav walker class to fully implement the Twitter Bootstrap 4.0+ navigation style (v3-branch available for Bootstrap 3) in a custom theme using the WordPress built in menu manager.
https://wp-bootstrap.github.io/wp-bootstrap-navwalker/
GNU General Public License v3.0
3.37k stars 1.94k forks source link

Update README.md #495

Open maheraldous opened 4 years ago

maheraldous commented 4 years ago

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 commented 4 years ago

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

maheraldous commented 4 years ago

@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 commented 4 years ago

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 commented 3 years ago

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