我以前的插件将注释保存为type=“wp\\u review\\u comment”。我已经处理了前面的自定义字段,如下所示。但我很难将注释保存为自定义注释类型。
// Add default fields
add_filter(\'comment_form_default_fields\',\'custom_fields\');
function custom_fields($fields) {
$commenter = wp_get_current_commenter();
$req = get_option( \'require_name_email\' );
$aria_req = ( $req ? " aria-required=\'true\'" : \'\' );
$fields[ \'author\' ] = \'<p class="comment-form-author">\'.
\'<label for="author">\' . __( \'Name\' ) . \'</label>\'.
( $req ? \'<span class="required">*</span>\' : \'\' ).
\'<input id="author" name="author" type="text" value="\'. esc_attr( $commenter[\'comment_author\'] ) .
\'" size="30" tabindex="1"\' . $aria_req . \' /></p>\';
$fields[ \'email\' ] = \'<p class="comment-form-email">\'.
\'<label for="email">\' . __( \'Email\' ) . \'</label>\'.
( $req ? \'<span class="required">*</span>\' : \'\' ).
\'<input id="email" name="email" type="text" value="\'. esc_attr( $commenter[\'comment_author_email\'] ) .
\'" size="30" tabindex="2"\' . $aria_req . \' /></p>\';
$fields[ \'url\' ] = \'<p class="comment-form-url">\'.
\'<label for="url">\' . __( \'Website\' ) . \'</label>\'.
\'<input id="url" name="url" type="text" value="\'. esc_attr( $commenter[\'comment_author_url\'] ) .
\'" size="30" tabindex="3" /></p>\';
return $fields;
}
// Add fields.
add_action( \'comment_form_logged_in_after\', \'additional_fields\' );
add_action( \'comment_form_after_fields\', \'additional_fields\' );
function additional_fields () {
echo \'<p class="comment-form-title">\'.
\'<label for="wp_review_comment_title">\' . __( \'Titley\' ) . \'</label>\'.
\'<input id="wp_review_comment_title" name="wp_review_comment_title" type="text" size="30" tabindex="5" /></p>\';
echo \'<p class="comment-form-rating">\'.
\'<label for="wp_review_comment_rating">\'. __(\'Ratingy: \') . \'<span class="required">*</span></label>
<span class="commentratingbox">\';
for( $i=1; $i <= 5; $i++ )
echo \'<span class="commentrating"><input type="radio" name="wp_review_comment_rating" id="rating" value="\'. $i .\'"/>\'. $i .\'</span>\';
echo\'</span></p>\';
}
// Save the comment metadata along with the comment.
add_action( \'comment_post\', \'save_comment_meta_data\' );
function save_comment_meta_data( $comment_id ) {
if ( ( isset( $_POST[\'wp_review_comment_title\'] ) ) && ( $_POST[\'wp_review_comment_title\'] != \'\') )
$title = wp_filter_nohtml_kses($_POST[\'wp_review_comment_title\']);
add_comment_meta( $comment_id, \'wp_review_comment_title\', $title );
if ( ( isset( $_POST[\'wp_review_comment_rating\'] ) ) && ( $_POST[\'wp_review_comment_rating\'] != \'\') )
$rating = wp_filter_nohtml_kses($_POST[\'wp_review_comment_rating\']);
add_comment_meta( $comment_id, \'wp_review_comment_rating\', $rating );
}
// Check if the comment metadata has been filled or not
add_filter( \'preprocess_comment\', \'verify_comment_meta_data\' );
function verify_comment_meta_data( $commentdata ) {
if ( ! isset( $_POST[\'wp_review_comment_rating\'] ) )
wp_die( __( \'A rating is requird. Hit the BACK button and resubmit your review with a rating.\' ) );
return $commentdata;
}
现在,我的注释保存为默认注释类型=“注释”。如何将注释保存为自定义注释类型“wp\\u review\\u comment”?
最合适的回答,由SO网友:Mikhail 整理而成
此外您已经有了可以满足您需要的代码。
add_filter( \'preprocess_comment\', \'verify_comment_meta_data\' );
function verify_comment_meta_data( $commentdata ) {
if ( ! isset( $_POST[\'wp_review_comment_rating\'] ) )
wp_die( __( \'A rating is required. Hit the BACK button and resubmit your review with a rating.\' ) );
// this is filter and $commentdata has comment_type parameter in it. so you need to do this
$commentdata[\'comment_type\'] = \'wp_review_comment\';
return $commentdata;
}
这将满足您的需要,但现在所有新创建的注释都将成为这种类型的
wp_review_comment
, 因此,如果您以另一种方式需要它们,那么您需要在这个过滤器中使用某种条件,而不是强制所有注释都使用
wp_review_comment_rating
强迫他们
wp_review_comment
类型或模具(),阻止保存。
看见https://core.trac.wordpress.org/browser/tags/5.3/src/wp-includes/comment.php#L1874 有关更多详细信息
SO网友:Asha
$data = array(
\'comment_post_ID\' => $post_id,
\'comment_author\' => \'admin\',
\'comment_content\' => $comment,
\'user_id\' => $current_user->ID,
\'comment_date\' => $time,
\'comment_approved\' => 1,
\'comment_type\' => \'custom-comment-class\'
);
wp_insert_comment($data);