Nav Menu Short code Not working When non Login

The issue you’re facing, where the shortcode appears as text instead of being executed for users who are not logged in, could be due to the timing of when the do_shortcode function is applied to the menu items. The wp_nav_menu filter is not the ideal place to apply do_shortcode, as it can lead to unexpected behavior, like the one you’re experiencing.

A better approach is to use the wp_nav_menu_items filter, which allows you to append or prepend additional items to your menu. Here, you can directly insert the output of your shortcode.

Here’s how you can modify your code:

Remove your current filter that applies do_shortcode on wp_nav_menu.

In your theme’s functions.php file add a new filter to wp_nav_menu_items like this:

add_filter('wp_nav_menu_items', 'add_dark_light_toggle_to_menu', 10, 2);
function add_dark_light_toggle_to_menu($items, $args) {
    if ($args->theme_location == 'primary-menu') {
        $toggle_button = do_shortcode('[dark_light_toggle_button]');
        $items .= '<li class="menu-item">' . $toggle_button . '</li>';
    }
    return $items;
}

This code checks if the menu is the primary menu (adjust ‘primary-menu’ if your theme uses a different location name). It then appends the output of your [dark_light_toggle_button] shortcode as a new menu item.

Ensure that your shortcode function dark_light_toggle_button_shortcode and the add_dark_light_toggle_to_menu function are both available to all users, regardless of their login status.

By using the wp_nav_menu_items filter, you’re directly manipulating the menu items, and this should resolve the issue of the shortcode appearing as text for non-logged-in users. This approach is more consistent and should work across different themes and setups.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)