0

I'm learning PHP by building a plugin to show or hide menu items based on a user's location. I am a bit stuck at the moment.

In the function below, the text input is being saved to the database, but I'm having trouble saving the radio button data to the database.

I want to the first option to be checked by default.

    function option( $fields, $item_id ) {
    ob_start(); ?>
        <p class="field-visibility description description-wide">
            <label for="edit-menu-item-visibility-<?php echo $item_id; ?>">
                <?php _e('Enter country code(s) separated with commas') ?>:
                <input type="text" 
                        class="widefat code" 
                        id="edit-menu-item-visibility-<?php echo $item_id; ?>" 
                        name="menu-item-visibility[<?php echo $item_id; ?>]" 
                        value="<?php echo esc_html( get_post_meta( $item_id, 'locations', true ) ); ?>" /></br>
                <input type="radio" 
                        id="edit-menu-item-visibility-<?php echo $item_id; ?>" 
                        name="menu-item-show-hide[<?php echo $item_id; ?>]" 
                        value="<?php echo get_post_meta( $item_id, 'hide', true ); ?>" 
                        />Hide from these locations.</br>
                <input type="radio" 
                        id="edit-menu-item-visibility-<?php echo $item_id; ?>"
                        name="menu-item-show-hide[<?php echo $item_id; ?>]" 
                        value="<?php echo get_post_meta( $item_id, 'show', true ); ?>" 
                        />Only show to these locations.</br>
            </label>
        </p>
    <?php
    $fields[] = ob_get_clean();
    return $fields;
    }

Here is the current output:

enter image description here

Ideally, I would use the radio buttons to set a variable called $visible true or false. But I wasn't sure how to accomplish this.

Thanks in advance!

1

Use the checked function. https://codex.wordpress.org/Function_Reference/checked

The HTML you want to render should look like this:

<input ... value="hide" checked="checked" />Hide from these locations.</br>
<input ... value="show" />Only show to these locations.</br>

What you're getting instead is probably somthing like this:

<input ... value="hide" />Hide from these locations.</br>
<input ... value="" />Only show to these locations.</br>

This should help:

<input type="radio" id="edit-menu-item-visibility-<?php echo $item_id;>" name="menu-item-show-hide[<?php echo $item_id; ?>]" value="hide" <?php checked( get_post_meta( $item_id, 'hide_show', true ), 'hide', true ); ?>" />Hide from these locations.</br>
<input type="radio"id="edit-menu-item-visibility-<?php echo $item_id; ?>" name="menu-item-show-hide[<?php echo $item_id; ?>]" value="show" <?php checked( get_post_meta( $item_id, 'hide_show', true ), 'show', true ); ?>" />

Only show to these locations.

  • Thanks, Charles. You put me on the right track and it's working now. – j8d Feb 9 '16 at 11:58

Your Answer

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

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