admin管理员组

文章数量:1318564

on our wordpress site we use utm_source in the querystring to determine the campaign that brought a customer to our site.

I need to dynamically add the passed utm_source from the initial page to all the menu items on the site. How can I modify all links in the wordpress menus to add the querystring variables?

on our wordpress site we use utm_source in the querystring to determine the campaign that brought a customer to our site.

I need to dynamically add the passed utm_source from the initial page to all the menu items on the site. How can I modify all links in the wordpress menus to add the querystring variables?

Share Improve this question edited Oct 19, 2020 at 17:36 afshin asked Oct 19, 2020 at 17:05 afshinafshin 1033 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 2

Can be changed using filter

add_filter('wp_get_nav_menu_items', 'add_utm_to_links', 10, 3);

function add_utm_to_links($items, $menu, $args) {

    foreach($items as $item) {
        if(!empty($item->url)) {
            $item->url .=  strchr($url, '?') === false ? '?' : '&';  
            $item->url .= 'utm=value';
        }
    }

    return $items;
}

本文标签: theme customizermodify menu item links dynamically