admin管理员组文章数量:1390749
My previous plugin was saving the comments as type="wp_review_comment". I have handled the previous custom fields as below. But I'm having difficulty saving the comments as a custom comment type.
// 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;
}
Now, my comments are saved as default comment-type="comment". How do I save the comment as a custom comment type "wp_review_comment"?
My previous plugin was saving the comments as type="wp_review_comment". I have handled the previous custom fields as below. But I'm having difficulty saving the comments as a custom comment type.
// 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;
}
Now, my comments are saved as default comment-type="comment". How do I save the comment as a custom comment type "wp_review_comment"?
Share Improve this question edited Mar 9, 2020 at 3:52 Bikram asked Mar 8, 2020 at 13:39 BikramBikram 3386 silver badges22 bronze badges 1- I think this link will resolve your issue - stackoverflow/questions/21404466/… – Asha Commented Mar 12, 2020 at 7:41
2 Answers
Reset to default 1In addition. You already have code which can kind of do what you need.
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;
}
This will do what you wanted but now all your newly created comments will become this type of wp_review_comment
, so if you need them the other way - you'll need some kind of conditionals in this filter instead of forcing all comments to have wp_review_comment_rating
and forcing them to be wp_review_comment
type or die(), preventing from saving.
See https://core.trac.wordpress/browser/tags/5.3/src/wp-includes/comment.php#L1874 for more details
$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);
本文标签: How to save new comment as custom comment type
版权声明:本文标题:How to save new comment as custom comment type? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744676192a2619125.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论