L
L
ligisayan2020-09-07 16:13:57
metadata
ligisayan, 2020-09-07 16:13:57

How to add fields to an existing wordpress meta box?

Hello!
There is a site on wordpress with a comment form. I add 2 meta fields job and city

add_action( 'comment_form_logged_in_after', 'extend_comment_custom_fields' );
add_action( 'comment_form_after_fields', 'extend_comment_custom_fields' );
function extend_comment_custom_fields() {

    echo '<p class="comment-form-job">'.
              '<label for="job">' . esc_attr__( 'Ваша профессия','credit7') . '</label>'.
              '<input id="job" name="job" type="text" size="30"/></p>';

    echo '<p class="comment-form-city">'.
              '<label for="city">' . esc_attr__( 'Город проживания','credit7') . '</label>'.
              '<input id="city" name="city" type="text" size="30"/></p>';
}

add_action( 'comment_post', 'save_extend_comment_meta_data' );
function save_extend_comment_meta_data( $comment_id ){

    if( !empty( $_POST['job'] ) ){
        $job = sanitize_text_field($_POST['job']);
        add_comment_meta( $comment_id, 'job', $job );
    }

    if( !empty( $_POST['city'] ) ){
        $city = sanitize_text_field($_POST['city']);
        add_comment_meta( $comment_id, 'city', $city );
    }

}


Now I need to display them in the admin panel. If you add like this:

add_action( 'add_meta_boxes_comment', 'extend_comment_add_meta_box' );
function extend_comment_add_meta_box(){
    add_meta_box( 'title', __( 'Comment Metadata - Extend Comment' ), 'extend_comment_meta_box', 'comment', 'normal', 'high' );
}

// Отображаем наши поля
function extend_comment_meta_box( $comment ){
    $job  = get_comment_meta( $comment->comment_ID, 'job', true );
    $city  = get_comment_meta( $comment->comment_ID, 'city', true );

    wp_nonce_field( 'extend_comment_update', 'extend_comment_update', false );
    ?>
    <p>
        <label for="job"><?php esc_attr__( 'Ваша профессия','credit7'); ?></label>
        <input type="text" name="job" value="<?php echo esc_attr( $job ); ?>" class="widefat" />
    </p>
    <p>
        <label for="city"><?php esc_attr__( 'Город проживания','credit7'); ?></label>
        <input type="text" name="city" value="<?php echo esc_attr( $city ); ?>" class="widefat" />
    </p>
    <?php
}


Then they fall into a separate metabox. But how to add them to an existing metabox (on the screen) so that they follow one after another and are not divided into blocks?
5f563212e8040877694548.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Boris Cherepanov, 2020-09-07
@xakplant

You just need to use the same meta-box idadd_meta_box( ID_META_BOX, ..... );

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question