• I’m added new custom checkbox field to the nav menu items. the check box added without any problem when i check it it’s work but when i want uncheck it not work and stay in checked state.
    the code below show the steps to create the checkbox custom field>
    it is contains any error?

    first i created check box key within database

    function YPE_setup_custom_fields($item) {
        $item->divider = get_post_meta($item->ID, '_menu_item_divider', true);
    }
    add_filter('wp_setup_nav_menu_item', 'YPE_setup_custom_fields');

    then i created the update value for it

    function YPE_update_custom_fields($menu_id, $menu_item_db_id, $menu_item_data) {
        if (is_array($_REQUEST['menu-item-divider'])) {
            $YPE_divider_value = $_REQUEST['menu-item-divider'][$menu_item_db_id];
            update_post_meta($menu_item_db_id, '_menu_item_divider', $YPE_divider_value);
    }
    add_action('wp_update_nav_menu_item', 'YPE_update_custom_fields', 10, 3);

    then i created the new (Walker_Nav_Menu_Edit_Custom) class

    function YPE_edit_custom_walker($walker, $item_id) {
        return 'Walker_Nav_Menu_Edit_Custom';
    }
    add_filter('wp_edit_nav_menu_walker', 'YPE_edit_custom_walker', 10, 2);

    then i added the check box within my new class (Walker_Nav_Menu_Edit_Custom)

    <p class="field-link-divider description">
        <label for="edit-menu-item-divider-<?php echo $item_id; ?>">
            <input type="checkbox" id="edit-menu-item-divider-<?php echo $item_id; ?>" value="_blank" name="menu-item-divider[<?php echo $item_id; ?>]"<?php checked( $item->divider, '_blank' ); ?> />
            <?php _e( 'Show divider' ); ?>
        </label>
    </p>
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)

The topic ‘Adding custom fields to WordPress nav menus’ is closed to new replies.