Skip to content

Instantly share code, notes, and snippets.

@lunule
Last active May 14, 2024 18:33
Show Gist options
  • Save lunule/914b81b32843e18daade2826091123e7 to your computer and use it in GitHub Desktop.
Save lunule/914b81b32843e18daade2826091123e7 to your computer and use it in GitHub Desktop.
[WP - Prefix CUSTOM Menu Items Beginning with an '/' Character with the Site Url] #wordpress #wp #core #menu #prefix #site #url #site_url #custom
<?php
// Automatically add site url to menu CUSTOM links
// Automatically add site url to menu CUSTOM links
function jd_prefix_custom_menu_items( $items, $args ) {
// loop through menu-objects (the links)
foreach ($items as $key => $item) :
// check if link begins with '/'
if ( $item->object == 'custom' && substr($item->url, 0, 1) == '/' ) {
// if so, prepend site_url to link
$item->url = site_url() . $item->url;
}
endforeach;
// return edited links
return $items;
}
add_filter('wp_nav_menu_objects', 'jd_prefix_custom_menu_items', 10, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment