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?


1 Answer 1


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;
  • Thanks this was very helpful. I added a condition to not add any characters when there are no querystring variables.
    – afshin
    Oct 20, 2020 at 15:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.