Skip to content

Instantly share code, notes, and snippets.

@IlanVivanco
Last active March 16, 2021 12:55
Show Gist options
  • Save IlanVivanco/26b18d20f20c3fec127f995e44a95d37 to your computer and use it in GitHub Desktop.
Save IlanVivanco/26b18d20f20c3fec127f995e44a95d37 to your computer and use it in GitHub Desktop.
Add a shortcode to a specific menu item
<?php
/**
* Filters all menu item URLs for a #placeholder#.
*
* @param WP_Post[] $menu_items All of the nave menu items, sorted for display.
*
* @return WP_Post[] The menu items with any placeholders properly filled in.
*/
function iv_dynamic_menu_items( $menu_items ) {
foreach ( $menu_items as $menu_item ) {
if ( '#profile_link#' === $menu_item->url ) {
global $shortcode_tags;
$shortcode = 'my_shortcode';
if ( isset( $shortcode_tags[ $shortcode ] ) ) {
$atts = array();
$content = '';
$menu_item->url = call_user_func(
$shortcode_tags[ $shortcode ],
$atts,
$content,
$shortcode
);
}
}
}
return $menu_items;
}
add_filter( 'wp_nav_menu_objects', 'iv_dynamic_menu_items' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment