Created
August 7, 2020 06:44
-
-
Save BruceMcKinnon/7fb238d6baf413cb2a9cf551ca2f216f to your computer and use it in GitHub Desktop.
Dynamic navs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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