How to add checkbox to advanced menu properties WordPress

To add a checkbox in the extended properties of a menu in WordPress, you need to use hooks and WordPress API functions to create and process additional fields. Here’s an example of how to do this:

Adding a checkbox to the menu edit form

Use the hook wp_nav_menu_item_custom_fields to add the field in the menu edit form:

add_action('wp_nav_menu_item_custom_fields', 'add_custom_checkbox_field', 10, 4);

function add_custom_checkbox_field($item_id, $item, $depth, $args) {
    $custom_checkbox = get_post_meta($item_id, '_custom_checkbox', true);
    ?>
    <p class="field-custom field-custom-checkbox description-wide">
        <label for="edit-menu-item-custom-checkbox-<?php echo $item_id; ?>">
            <input type="checkbox" id="edit-menu-item-custom-checkbox-<?php echo $item_id; ?>" name="menu-item-custom-checkbox[<?php echo $item_id; ?>]" value="1" <?php checked($custom_checkbox, 1); ?> />
            <?php _e('Custom Checkbox', 'text-domain'); ?>
        </label>
    </p>
    <?php
}

Saving the checkbox value

Use the hook wp_update_nav_menu_item to save the checkbox value when saving the menu:

add_action('wp_update_nav_menu_item', 'save_custom_checkbox_field', 10, 3);

function save_custom_checkbox_field($menu_id, $menu_item_db_id, $args) {
    if (isset($_POST['menu-item-custom-checkbox'][$menu_item_db_id])) {
        $custom_checkbox = 1;
    } else {
        $custom_checkbox = 0;
    }
    update_post_meta($menu_item_db_id, '_custom_checkbox', $custom_checkbox);
}

Using the checkbox value

If you want to use the checkbox value in your menu template, you can retrieve it using get_post_meta:

$custom_checkbox = get_post_meta($item->ID, '_custom_checkbox', true);
if ($custom_checkbox) {
    // Your code to display or use the checkbox
}

This code adds a checkbox to the extended properties section of each menu item, saves its value, and allows you to use this value in your template. Don’t forget to replace 'text-domain' with your text domain.

How useful is the publication?

Click on a star to rate it!

Average score 5 / 5. Number of grades: 1

No ratings yet. Rate it first.

Similar posts

Why Files with Identical Content (*.js, *.php, *.css) Can Have Different Sizes?

When developers compare files with identical content but notice that their sizes differ, it can be perplexing. Let’s explore why this happens and what factors influence the size of files with extensions like *.js, *.php, and *.css. 1. File Encoding One of the key factors affecting file size is text encoding. The most common encodings…
Read more

How to transfer a site from dle to WordPress?

Transferring a website from DLE (DataLife Engine) to WordPress can be a complex process, especially if the site has a lot of content. Here’s a step-by-step guide: 1. Preparation 2. Export Data from DLE DLE uses its own database structure, so you’ll need to export data and convert it into a format compatible with WordPress:…
Read more