0

I've created my own set of menu attributes

function menu_post_meta_fields( $menu_item ) {  

    $menu_item->icon    = get_post_meta( $menu_item->ID, '_menu_item_icon', true);
    $menu_item->hide    = get_post_meta( $menu_item->ID, '_menu_item_hide', true ); 

    //... and so on.

    return $menu_item;

}

and use it like this:

function menu_fields_save_and_update( $menu_id, $menu_item_db_id, $args ) {

if ( ! empty($_REQUEST['menu-item-icon'][$menu_item_db_id]) && isset($_REQUEST['menu-item-icon']) && is_array($_REQUEST['menu-item-icon'])) {
    $custom_value = $_REQUEST['menu-item-icon'][$menu_item_db_id];
    update_post_meta($menu_item_db_id, '_menu_item_icon', $custom_value);
} else {
    $custom_value = '';
    update_post_meta($menu_item_db_id, '_menu_item_icon',$custom_value);
} 

if ( !empty($_REQUEST['menu-item-hide'][$menu_item_db_id]) && isset($_REQUEST['menu-item-hide']) && is_array($_REQUEST['menu-item-hide'])) {
    $custom_value = $_REQUEST['menu-item-hide'][$menu_item_db_id];
    update_post_meta($menu_item_db_id, '_menu_item_hide', $custom_value);
}  else {
    $custom_value = '';
    update_post_meta($menu_item_db_id, '_menu_item_hide',$custom_value);
} 
// and so on.   
}
add_action('wp_update_nav_menu_item' , 'menu_fields_save_and_update' );

This is fine if I would have just a handful of custom fields, but not when I created more than 50+. Is there a more efficient way to code this? For exampe with two functions and a loop?

I'd like to create 2 functions

function menu_db_key() {     
    $key_one = array(
        '_menu_item_icon',
        '_menu_item_hide'
    );      
}

function menu_input_field_name() {     
    $key_two = array(
        'menu-item-icon',
        'menu-item-hide'
    );    
}

Also, I noticed a huge spike in queries when saving a menu. Caller is always "update_meta_cache" and related to wp_get_nav_menu_items & wp_nav_menu_update_menu_items. Was/Is there anyone in a similiar situation?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.