Dynamically change WordPress menu label

In this article, I will show you how you can utilize the wp_nav_menu_items hook to work with a specific menu item of a specific WordPress menu. You need to have a custom menu enabled in your themes before you can proceed any further.

How to change WordPress menu label

Use the following filter function to change menu label dynamically. This filter function use the PHP str_replace function to search and replace.


add_filter( 'wp_nav_menu_items', 'dynamic_label_change', 10, 2 ); 
function dynamic_label_change( $items, $args ) { 

  if (!is_user_logged_in() && $args->theme_location == 'topbar_navigation') { 
    $items = str_replace("Login", "Profile", $items); 
  } 

return $items; 
} 

As you can see, you can use the conditional statements along with the theme_location used in argument. This also allows you to target a specific menu location. This is a basic example, make changes to meet your requirement.

There is another way to do this using getttext.

About This Author

My name is Parameshwar Roy (P. Roy), and I am a web developer. This is my personal blog to record my own thoughts. Though I am not a natural writer, I love to share my experiences. Hope my experiences will be useful to you…read more about me

Leave A Reply