
<?phpp
/**
 * Customize WordPress Menu Visibility Based on User Login Status
 *
 * This function filters menu items to show or hide them based on whether the user is logged in.
 * 
 * Usage Instructions:
 * 1. **Add CSS Classes to Menu Items:**
 *    - Navigate to **Appearance > Menus** in your WordPress dashboard.
 *    - Click on **Screen Options** at the top-right and ensure **CSS Classes** is checked.
 *    - For any menu item you want to control:
 *      - Add `logged-in` to show the item only to logged-in users.
 *      - Add `logged-out` to show the item only to logged-out (guest) users.
 *
 * 2. **Implement the Code:**
 *    - Add this code to your theme's `functions.php` file or a [site-specific plugin](https://www.wpbeginner.com/wp-tutorials/how-to-create-a-site-specific-wordpress-plugin-for-custom-code/).
 *
 * 3. **Test the Functionality:**
 *    - Log in and log out of your website to ensure menu items appear or disappear as configured.
 *
 * Note:
 * - Ensure that sensitive pages are also protected server-side, not just hidden from the menu.
 * - If using a caching plugin, clear the cache to reflect the changes immediately.
 */

function custom_wp_nav_menu_objects( $sorted_menu_items, $args ) {
    // Initialize an array to hold the filtered menu items
    $filtered_menu = array();

    // Determine user login status
    $is_logged_in = is_user_logged_in();

    foreach ( $sorted_menu_items as $menu_item ) {
        // Check if the menu item has CSS classes
        if ( in_array( 'logged-in', $menu_item->classes ) ) {
            // Show only to logged-in users
            if ( $is_logged_in ) {
                $filtered_menu[] = $menu_item;
            }
            // Skip to next item
            continue;
        }

        if ( in_array( 'logged-out', $menu_item->classes ) ) {
            // Show only to logged-out users
            if ( ! $is_logged_in ) {
                $filtered_menu[] = $menu_item;
            }
            // Skip to next item
            continue;
        }

        // If no specific classes are set, always display the menu item
        $filtered_menu[] = $menu_item;
    }

    return $filtered_menu;
}
add_filter( 'wp_nav_menu_objects', 'custom_wp_nav_menu_objects', 10, 2 );
<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="https://costacarts.com/sitemaps_xsl.xsl"?>
<sitemapindex xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/siteindex.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<sitemap>
<loc>https://costacarts.com/post-sitemap1.xml</loc>
<lastmod>2026-03-31T09:10:32-05:00</lastmod>
</sitemap>
<sitemap>
<loc>https://costacarts.com/page-sitemap1.xml</loc>
<lastmod>2026-03-16T13:47:00-05:00</lastmod>
</sitemap>
<sitemap>
<loc>https://costacarts.com/product-sitemap1.xml</loc>
<lastmod>2026-04-02T11:34:48-05:00</lastmod>
</sitemap>
<sitemap>
<loc>https://costacarts.com/category-sitemap1.xml</loc>
</sitemap>
</sitemapindex>