Using WordPress ‘nav_menu_description’ PHP filter

nav_menu_description is a WordPress PHP filter that allows you to modify a navigation menu item’s description.

Usage

add_filter('nav_menu_description', 'your_custom_function');
function your_custom_function($description) {
  // your custom code here
  return $description;
}

Parameters

  • $description string: The menu item description you want to modify.

More information

See WordPress Developer Resources: https://developer.wordpress.org/reference/hooks/nav_menu_description/

Examples

Uppercase Menu Description

Changes the menu description to uppercase.

add_filter('nav_menu_description', 'uppercase_menu_description');
function uppercase_menu_description($description) {
  $description = strtoupper($description);
  return $description;
}

Add a Prefix to Menu Description

Adds a prefix to the menu description.

add_filter('nav_menu_description', 'add_prefix_to_description');
function add_prefix_to_description($description) {
  $prefix = 'Info: ';
  $description = $prefix . $description;
  return $description;
}

Truncate Menu Description

Truncate the menu description to a specific length.

add_filter('nav_menu_description', 'truncate_menu_description');
function truncate_menu_description($description) {
  $max_length = 50;
  $description = substr($description, 0, $max_length);
  return $description;
}

Replace Specific Text in Menu Description

Replaces specific text within the menu description.

add_filter('nav_menu_description', 'replace_text_in_description');
function replace_text_in_description($description) {
  $search = 'old text';
  $replace = 'new text';
  $description = str_replace($search, $replace, $description);
  return $description;
}

Remove HTML Tags from Menu Description

Strips any HTML tags from the menu description.

add_filter('nav_menu_description', 'remove_html_from_description');
function remove_html_from_description($description) {
  $description = strip_tags($description);
  return $description;
}