Skip to content

Instantly share code, notes, and snippets.

@BruceMcKinnon
Created August 7, 2020 06:44
Show Gist options
  • Save BruceMcKinnon/7fb238d6baf413cb2a9cf551ca2f216f to your computer and use it in GitHub Desktop.
Save BruceMcKinnon/7fb238d6baf413cb2a9cf551ca2f216f to your computer and use it in GitHub Desktop.
Dynamic navs
/*
* Filters all menu item URLs for a ##anchor-link
*/
function bl_dynamic_menu_items( $menu_items ) {
$current_page = "http://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
foreach ( $menu_items as $menu_item ) {
// All the blcontact shortcodes to be used as the URL of custom links.
// Must include a ## before the shortcode. E.g., ##[blcontact type='misc1']
if ( startsWith($menu_item->url, '##%5Bblcontact') && endsWith($menu_item->url, '%5D') ) {
$menu_item->url = str_ireplace("##",'',$menu_item->url);
$shortcode = urldecode($menu_item->url);
//fb_log('sc: '.$shortcode);
$menu_item->url = do_shortcode($shortcode);
} else {
if ( startsWith($menu_item->url, '##url##/') ) {
$urllen = strlen($menu_item->url);
$menu_item->url = get_bloginfo('url') . '/' . substr($menu_item->url,8,$urllen-8);
}
}
$menu_item->url = str_ireplace("##", "#", $menu_item->url);
}
return $menu_items;
}
add_filter( 'wp_nav_menu_objects', 'bl_dynamic_menu_items' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment