禁止使用模板插件做违法内容(涉黄等),否则将受到法律的制裁。

WordPress hook钩子comment_id_fields的用法详解

禁止使用模板插件做违法内容(涉黄等),否则将受到法律的制裁。

WordPress中的comment_id_fields钩子是在评论表单中的comment_id字段之前添加自定义字段的地方。这个钩子接受一个参数,即当前评论表单的数据对象。

在这个钩子中,你可以添加自定义的HTML字段,用于收集额外的评论信息。这些字段的值将保存为评论的元数据。

下面是一个示例,展示了如何使用comment_id_fields钩子添加一个自定义字段:

function custom_comment_fields($comment){
?>


<input type="text" name="custom_field" id="custom_field" value="comment_ID, 'custom_field', true); ?>" />

<?php
}
add_action('comment_id_fields', 'custom_comment_fields');

在上述示例中,我们在评论表单中添加了一个名为“custom_field”的自定义字段。使用get_comment_meta函数,我们还可以在字段中显示评论的元数据(如果有的话)。

要保存这个自定义字段的值,你可以使用comment_post钩子:

function save_custom_comment_field($comment_id){
if(isset($_POST['custom_field'])){
$custom_field_value = sanitize_text_field($_POST['custom_field']);
update_comment_meta($comment_id, 'custom_field', $custom_field_value);
}
}
add_action('comment_post', 'save_custom_comment_field');

在这个钩子中,我们首先检查是否存在名为“custom_field”的POST参数。如果存在,我们使用sanitize_text_field函数对值进行过滤,然后使用update_comment_meta函数将其保存为评论的元数据。

通过使用comment_id_fields钩子和comment_post钩子,你可以在WordPress评论系统中添加自定义字段,并将其保存为评论的元数据。这样,你可以收集额外的评论信息,并在需要时使用它们。

0 个评论

 
联系
目前暂停一切业务,客服消息可能不会及时回复,谢谢理解。
在线咨询
    在线咨询
    禁止使用模板插件做违法内容(涉黄等),否则将受到法律的制裁。