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:
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!
