0

So I have the following structure on the post editor with my custom post type:

features_metabox:
  - option1 []
  - option2 []
  - option3 []


metabox_opt1
 -field1

metabox_opt2
 -field2

metabox_opt3
 -field3

what I want is to display the metabox_op1 when the op1 checkbox is marked in features_metabox, I know how to do it with javascript, but I'm in WP and I have no idea how to do it with php, have anyone did that before?

1

Why don't you use only one metabox and then use jQuery to manage what options to show:

// add metabox
function adding_custom_meta_boxes( $post_type, $post ) {
    add_meta_box(
        'my-meta-box',
        __( 'My Meta Box' ),
        'render_my_meta_box',
        'post',
        'normal',
        'default'
    );
}
add_action( 'add_meta_boxes', 'adding_custom_meta_boxes', 10, 2 );

// render metabox
function render_my_meta_box( $post ) {
    wp_nonce_field( 'my_metas_nonce', 'my_metas_nonce' );

    $my_features_1 = get_post_meta( $post->ID, 'my_features_1', true );
    $my_option_1 = get_post_meta( $post->ID, 'my_option_1', true );
    $my_features_2 = get_post_meta( $post->ID, 'my_features_2', true );
    $my_option_2 = get_post_meta( $post->ID, 'my_option_2', true );
    $my_features_3 = get_post_meta( $post->ID, 'my_features_3', true );
    $my_option_3 = get_post_meta( $post->ID, 'my_option_3', true );

    ?>
        <input type="checkbox" name="my_features_1" class="my_features" value="my_option_1" <?php checked($my_features_1, 'my_option_1'); ?>>Option 1<br>
        <input type="checkbox" name="my_features_2" class="my_features" value="my_option_2" <?php checked($my_features_2, 'my_option_2'); ?>>Option 2<br>
        <input type="checkbox" name="my_features_3" class="my_features" value="my_option_3" <?php checked($my_features_3, 'my_option_3'); ?>>Option 3<br>

        <div id="my_option_1" class="my_options" style="<?php if( ! $my_features_1 ) echo 'display: none;'; ?>">
            <h3>Option 1</h3>
            <input name="my_option_1" value="<?php echo $my_option_1; ?>" />
        </div>

        <div id="my_option_2" class="my_options" style="<?php if( ! $my_features_2 ) echo 'display: none;'; ?>">
            <h3>Option 2</h3>
            <input name="my_option_2" value="<?php echo $my_option_2; ?>" />
        </div>

        <div id="my_option_3" class="my_options" style="<?php if( ! $my_features_3 ) echo 'display: none;'; ?>">
            <h3>Option 3</h3>
            <input name="my_option_3" value="<?php echo $my_option_3; ?>" />
        </div>

        <script>
            jQuery(document).ready(function($) {
                $('.my_features').change(function() {
                    var checkbox = $(this);
                    if( checkbox.is(':checked') ) {
                        $( '#' + checkbox.val() ).show();
                    } else {
                        $( '#' + checkbox.val() ).hide();
                    }
                });
            });
        </script>
    <?php
}

// save metabox data
function save_post_features_meta( $post_id ){
    if ( ! isset( $_POST['my_metas_nonce'] ) || ! wp_verify_nonce( $_POST['my_metas_nonce'], 'my_metas_nonce' ) || ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || ! current_user_can( 'edit_post', $post_id ) ) {
        return;
    }

    update_post_meta( $post_id, 'my_features_1', sanitize_text_field($_POST['my_features_1']));
    update_post_meta( $post_id, 'my_option_1', sanitize_text_field($_POST['my_option_1']));

    update_post_meta( $post_id, 'my_features_2', sanitize_text_field($_POST['my_features_2']));
    update_post_meta( $post_id, 'my_option_2', sanitize_text_field($_POST['my_option_2']));

    update_post_meta( $post_id, 'my_features_3', sanitize_text_field($_POST['my_features_3']));
    update_post_meta( $post_id, 'my_option_3', sanitize_text_field($_POST['my_option_3']));
}
add_action( 'save_post', 'save_post_features_meta');

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.