Wie zeige ich den Metafeldwert des Kommentars im Admin-Kommentarbereich an?

Lesezeit: 2 Minuten

Ich habe ein zusätzliches Feld im WordPress-Kommentarformular für das Benutzeralter hinzugefügt. Ich habe das Feld so hinzugefügt:

add_filter('comment_form_default_fields','add_comment_fields');
function add_comment_fields($fields) {
    $fields['location'] = '<p class="comment-form-location"><label for="location">' . __( Location ) . '</label>' .
        '<input id="location" name="location" type="text" size="30" /></p>';

    return $fields;

}

Außerdem habe ich den Metawert in der Kommentar-Metatabelle gespeichert, indem ich die Aktion „comment_post“ verwendet habe. Jetzt muss ich diesen Kommentar-Metawert im Admin-Kommentarbereich anzeigen. Wie kann ich das machen ?

Probieren Sie den folgenden Code aus, hoffen Sie, dass Sie Ihr zusätzliches Feld im Admin-Bereich sehen können (hier verwende ich das Alter als Metaschlüssel).

add_action( 'add_meta_boxes_comment', 'comment_add_meta_box' );
function comment_add_meta_box()
{
 add_meta_box( 'my-comment-title', __( 'Your field title' ), 'comment_meta_box_age',     'comment', 'normal', 'high' );
}

function comment_meta_box_age( $comment )
{
    $title = get_comment_meta( $comment->comment_ID, 'age', true );

   ?>
 <p>
     <label for="age"><?php _e( 'Your label Name' ); ?></label>;
     <input type="text" name="age" value="<?php echo esc_attr( $title ); ?>"  class="widefat" />
 </p>
 <?php
}
add_action( 'edit_comment', 'comment_edit_function' );
function comment_edit_function( $comment_id )
{
    if( isset( $_POST['age'] ) )
      update_comment_meta( $comment_id, 'age', esc_attr( $_POST['age'] ) );
}

Vielleicht sind diese Links hilfreich

(1) http://blog.ideashower.com/post/15147134893/wordpress-plugin-extra-comment-fields

(2) http://make.wordpress.org/docs/plugin-developer-handbook/10-plugin-components/custom-list-table-columns/

Wenn Sie den Metawert erfolgreich gespeichert haben, können Sie die verwenden get_comment_meta Funktion, um den Wert abzurufen.

Muster:

<?php $meta_values = get_comment_meta( $comment_id, $key, $single ); ?> 

in deinem Fall könnte das funktionieren:

<?php $meta_values = get_comment_meta( $comment_id, 'location', true ); ?> 

Weitere Informationen finden Sie in der vollständigen Dokumentation: http://codex.wordpress.org/Function_Reference/get_comment_meta

1312300cookie-checkWie zeige ich den Metafeldwert des Kommentars im Admin-Kommentarbereich an?

This website is using cookies to improve the user-friendliness. You agree by using the website further.

Privacy policy