Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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)?

share|improve this question
    
Could you be more specific about what you want to do with the results? Do you want to echo them in a theme file, store the configuration for later use, etc... –  Jeremy Jared Dec 14 '11 at 4:22
    
To get down to the brass tacks: How would you be able to prevent posts that are in a menu from being shown in the loop? –  Comradefuzz Dec 14 '11 at 6:29
add comment

2 Answers 2

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);
}
share|improve this answer
add comment

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.
}
share|improve this answer
    
This seems like a pretty good answer. I'll give it a try, but before I do, could you explain how using 'check_admin_referer' would be a misuse? –  Comradefuzz Dec 14 '11 at 6:24
1  
@Comradefuzz This action happens in a context of security, it is made to implement your own reactions to attackers and similar issues. Plus, the function is pluggable: it could be replaced by another plugin. And then the hook may not work anymore. Another solution would be to hook into 'updated_option' and search for 'theme_mods_' . get_option( 'stylesheet' )[get_theme_mods()]['nav_menu_locations']. But this is a long way, WordPress may change one aspect of it later, and your script may break too. –  toscho Dec 14 '11 at 7:47
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.