modify menu item links dynamically
-
21-04-2021 - |
Question
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?
Solution
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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange