modify menu item links dynamically

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 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;
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x