0

I have added some custom meta data for all my comments. I show this meta data in the backend. However, I would like to be able to insert metadata in the wp_comment_reply editor as well:

enter image description here

I thought I could use a wp_comment_reply filter for this, but I can't get it to work. Does anyone have an idea?

This is what I tried:

function wporg_more_comments( $content ) {
    echo '123';
    return $content;
}

add_filter( 'wp_comment_reply', 'wporg_more_comments', 5, 1 );

1 Answer 1

1

Considering WP hooks capabilities the only way to set extra content here is to reorganize reply/edit form itself, using hooks.

An example below demonstrates how to add extra HTML content on "reply" form. Find out "HERE IS YOUR CUSTOM CONTENT" label inside code to check where magic happens.

add_filter('wp_comment_reply', 'test_reply_comment_func', 10, 2);

function test_reply_comment_func($str, $input) {
    extract($input);
    $table_row = TRUE;
    if ($mode == 'single') {
        $wp_list_table = _get_list_table('WP_Post_Comments_List_Table');
    } else {
        $wp_list_table = _get_list_table('WP_Comments_List_Table');
    }
    // Get editor string
    ob_start();
    $quicktags_settings = array('buttons' => 'strong,em,link,block,del,ins,img,ul,ol,li,code,spell,close');
    wp_editor('', 'replycontent', array('media_buttons' => false, 'tinymce' => false, 'quicktags' => $quicktags_settings, 'tabindex' => 104));
    $editorStr = ob_get_contents();
    ob_end_clean();`

    // Get nonce string
    ob_start();
    wp_nonce_field("replyto-comment", "_ajax_nonce-replyto-comment", false);
    if (current_user_can("unfiltered_html"))
        wp_nonce_field("unfiltered-html-comment", "_wp_unfiltered_html_comment", false);
    $nonceStr = ob_get_contents();
    ob_end_clean();


    $content = '<form method="get" action="">';
    if ($table_row) :
        $content .= '<table style="display:none;"><tbody id="com-reply"><tr id="replyrow" style="display:none;"><td colspan="' . $wp_list_table->get_column_count() . '" class="colspanchange">';
    else :
        $content .= '<div id="com-reply" style="display:none;"><div id="replyrow" style="display:none;">';
    endif;

    $content .= '<div id="replyhead" style="display:none;"><h5>Reply to Comment</h5>'
            . '<p style="margin:10px 0;"> HERE IS YOUR CUSTOM CONTENT</p>'
            . '</div>';


    $content .= '<div id="addhead" style="display:none;"><h5>Add new Comment</h5></div>
            <div id="edithead" style="display:none;">';

    $content .= '   
                <div class="inside">
                <label for="author">Name</label>
                <input type="text" name="newcomment_author" size="50" value="" tabindex="101" id="author" />
                </div>

                <div class="inside">
                <label for="author-email">E-mail</label>
                <input type="text" name="newcomment_author_email" size="50" value="" tabindex="102" id="author-email" />
                </div>

                <div class="inside">
                <label for="author-url">URL</label>
                <input type="text" id="author-url" name="newcomment_author_url" size="103" value="" tabindex="103" />
                </div>
                <div style="clear:both;"></div>';

    $content .= '</div>';

    // Add editor
    $content .= "<div id='replycontainer'>\n";
    $content .= $editorStr;

    $content .= "</div>\n";

    $content .= '           
            <p id="replysubmit" class="submit">
            <a href="#comments-form" class="cancel button-secondary alignleft" tabindex="106">Cancel</a>
            <a href="#comments-form" class="save button-primary alignright" tabindex="104">
            <span id="addbtn" style="display:none;">Add Comment</span>
            <span id="savebtn" style="display:none;">Update Comment</span>
            <span id="replybtn" style="display:none;">Submit Reply</span></a>
            <img class="waiting" style="display:none;" src="' . esc_url(admin_url("images/wpspin_light.gif")) . '" alt="" />
            <span class="error" style="display:none;"></span>
            <br class="clear" />
            </p>';

    $content .= '
            <input type="hidden" name="user_ID" id="user_ID" value="' . get_current_user_id() . '" />
            <input type="hidden" name="action" id="action" value="" />
            <input type="hidden" name="comment_ID" id="comment_ID" value="" />
            <input type="hidden" name="comment_post_ID" id="comment_post_ID" value="" />
            <input type="hidden" name="status" id="status" value="" />
            <input type="hidden" name="position" id="position" value="' . $position . '" />
            <input type="hidden" name="checkbox" id="checkbox" value="';

    if ($checkbox)
        $content .= '1';
    else
        $content .= '0';
    $content .= "\" />\n";
    $content .= '<input type="hidden" name="mode" id="mode" value="' . esc_attr($mode) . '" />';

    $content .= $nonceStr;
    $content .="\n";

    if ($table_row) :
        $content .= '</td></tr></tbody></table>';
    else :
        $content .= '</div></div>';
    endif;
    $content .= "\n</form>\n";
    return $content;
}

Result

enter image description here

Exaplanation.

This code overrides the default reply form code. The reply comment and edit comment internally connected. When you trigger to "quick edit" the <div> of .addhead and .edithead are visible, .replyhead - hidden. When you trigger to "reply" .addhead and .edithead are hidden, .replyhead - visible.

2
  • Thank you! What I really want to do, is place an input field where "HERE IS YOUR CUSTOM CONTENT" is. I figured out to do that by now. However, do you have any idea how I can save the content of this input field as meta data, when adding the comment? Sep 13, 2016 at 18:15
  • An old answer, but doesn't seem to work in WP 5.8.3, even if I use the wp_comment_reply code from that version. Dropdown doesn't show up on click of 'reply' or 'quick edit' links for a comment in the admin comment list. Using PHP 7.4.27. Jan 22, 2022 at 23:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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