if role is logged in, change url inside the “a href”

Question

This code works, but it’s going to get huge!
I have 150 “a href” to make and this is going to get rough, lol

Each role has a custom url.

How can I make this cleaner or smaller?

    if ( ! function_exists( 'add_extra_item_to_nav_menu' ) ){

    global $user_ID;

    function add_extra_item_to_nav_menu( $items, $args ) {
       if (current_user_can('administrator') && is_user_logged_in() && $args->menu-5) {
         $items .= '<li><a href="/product-category/public/SCHOOL_NAME-A/" class="navShopNow fungula">SHOP NOW ADMIN</a></li>';
       }
 
       // School ROLEABC
       elseif (current_user_can('ROLEABC') && is_user_logged_in && $args->menu-5) {
         $items .= '<li><a href="/product-category/public/SCHOOL_NAME-B/" class="navShopNow fungula">SHOP NOW ABC</a></li>';
       }
       // School ROLEXYZ
       elseif (current_user_can('ROLEXYZ') && is_user_logged_in && $args->menu-5) {
         $items .= '<li><a href="/product-category/public/SCHOOL_NAME-C/" class="navShopNow fungula">SHOP NOW XYZ</a></li>';
       }
 
       return $items;
    }
 
   add_filter( 'wp_nav_menu_items', 'add_extra_item_to_nav_menu', 150, 2 );
 }

0
Mike Loucas 4 months 2022-04-19T09:01:20-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse