Update Theme Location Programatically

Generally speaking, you shouldn’t be (or need to be) “updating” the theme_location for a custom nav menu. The theme_location is the template location for a given nav menu.

If you need to do dynamic manipulation of a custom nav menu, you might be better off targeting the id of the menu, rather than the theme_location. Your code above seems to be conflating the two. (Note that wp_create_nav_menu() accepts a menu name as an argument, rather than a menu theme location.)

But, what are you actually trying to do by dynamically manipulating menus in this way? Perhaps we can find a better solution.

EDIT

Based on your clarification: you definitely need to target the Menu Name, rather than the Theme Location.

One Theme Location correlates to one defined (physical) location within the Theme template. A Theme Location is not a menu itself, but rather a marker to indicate where a menu is output.

If you need to generate a default menu (and I’m really not sure why you need to do this; simply define a proper callback to wp_nav_menu() instead), then you need to pass the Menu Name to wp_create_nav_menu().