add_action for saving a custom menu

Say you have custom menus enabled with your WP theme. Is there any action associated with saving a menu once you’ve arranged it accordingly? To further clarify: say you’ve arranged a menu with some links and some posts, how might you get the titles of the posts in said menu upon saving (clicking the “Save Menu” button)?

Related posts

Leave a Reply

2 comments

  1. At least in 3.4.1, there is an action for that: wp_update_nav_menu

    See here.

    Then you can get the items in your menu with something like:

    add_action('wp_update_nav_menu', 'my_get_menu_items');
    function my_get_menu_items($nav_menu_selected_id) {
        $items = wp_get_nav_menu_items($nav_menu_selected_id);
    }
    
  2. There is no dedicated action for your needs, but you can misuse the 'check_admin_referer' hook. See the switch ( $action ) block in wp-admin/nav-menus.php for details and other options.

    Sample code for a start:

    add_action( 'check_admin_referer', 'check_nav_menu_updates', 11, 1 );
    
    function check_nav_menu_updates( $action )
    {
        if ( ( 'update-nav_menu' != $action ) or ! isset( $_POST['menu-locations'] ) )
        {
            return;
        }
    
        $nav_locations = $_POST['menu-locations'];
        $nav_title     = $_POST['menu-name'];
    
        // do something awesome with it.
    }