如何在WordPress中添加自定义注释功能?
请提供在WordPress中创建和管理自定义注释功能的方法。
-
玖比肆 评论要在WordPress中添加自定义注释功能,您可以使用以下方法:
第一步:打开functions.php文件
在WordPress主题的function.php文件中添加以下代码:// Add custom comment fields
function add_custom_comment_fields( $fields ) {$fields['website'] = '
' . esc_attr__( 'Website', 'domain' ) . '
';
return $fields;
}
add_filter( 'comment_form_default_fields', 'add_custom_comment_fields' );这将添加一个自定义网站字段到评论表单中。
第二步:保存评论元数据
添加以下代码以保存自定义注释元数据:// Save custom comment fields
function save_custom_comment_fields( $comment_id ) {
if ( ( isset( $_POST['website'] ) ) && ( $_POST['website'] != '') )
$website = wp_filter_nohtml_kses($_POST['website']);
add_comment_meta( $comment_id, 'website', $website );
}
add_action( 'comment_post', 'save_custom_comment_fields' );这将保存您的自定义注释元数据。
第三步:在注释栏中显示自定义注释元数据
添加以下代码以在注释栏中显示自定义注释元数据:// Display custom comment fields
function custom_comment_fields_display( $comment_text, $comment ) {$website = get_comment_meta( $comment->comment_ID, 'website', true );
if( ! empty( $website ) )
$comment_text .= '' . esc_attr__( 'Website:', 'domain' ) . '
' . $website . '';
return $comment_text;
}
add_filter( 'comment_text', 'custom_comment_fields_display', 20, 2 );这将在注释栏中显示您保存的自定义注释元数据。
第四步:管理注释元数据
您可以使用以下代码来管理您保存的自定义注释元数据:// Manage custom comment fields
function manage_comment_columns( $columns ) {
$columns['website'] = __( 'Website', 'domain' );
return $columns;
}
add_filter( 'manage_edit-comments_columns', 'manage_comment_columns' );function custom_comment_column( $column, $comment_ID ) {
if ( 'website' == $column ) {
$website = get_comment_meta( $comment_ID, 'website', true );
if( ! empty( $website ) )
echo '' . $website . '';
}
}
add_action( 'manage_comments_custom_column', 'custom_comment_column', 10, 2 );这将添加一个“网站”列,并在其中显示您保存的自定义注释元数据。
这是在WordPress中添加自定义注释功能的基本步骤。您可以根据自己的需求进行修改和定制。
2023年 5月 13日 上午5:04
