首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

将自定义字段/复选框添加到wordpress注释

将自定义字段/复选框添加到WordPress注释是通过使用WordPress的评论系统来扩展和自定义评论表单的功能。这可以让用户在发表评论时提供额外的信息或选择。

自定义字段是自定义注释字段,可以用于收集用户的额外信息。复选框是一种特殊类型的自定义字段,允许用户从多个选项中选择一个或多个选项。

以下是添加自定义字段/复选框到WordPress注释的步骤:

  1. 创建自定义字段:
    • 在WordPress主题的functions.php文件中添加以下代码来创建自定义字段:function custom_comment_fields($fields){ $fields['custom_field'] = '<p class="comment-form-custom-field">'. '<label for="custom_field">自定义字段</label>'. '<input id="custom_field" name="custom_field" type="text" value="" /></p>'; return $fields; } add_filter('comment_form_default_fields', 'custom_comment_fields');
    • 这将在评论表单中添加一个名为"自定义字段"的文本输入框。
  2. 创建复选框:
    • 在WordPress主题的functions.php文件中添加以下代码来创建复选框:function custom_comment_checkbox(){ echo '<p class="comment-form-custom-checkbox">'. '<input id="custom_checkbox" name="custom_checkbox" type="checkbox" value="1" />' . '<label for="custom_checkbox">复选框</label></p>'; } add_action('comment_form', 'custom_comment_checkbox');
    • 这将在评论表单中添加一个名为"复选框"的复选框。
  3. 保存和显示自定义字段/复选框的值:
    • 在WordPress主题的functions.php文件中添加以下代码来保存和显示自定义字段/复选框的值:function save_custom_comment_fields($comment_id){ if(isset($_POST['custom_field'])){ $custom_field_value = wp_filter_nohtml_kses($_POST['custom_field']); add_comment_meta($comment_id, 'custom_field', $custom_field_value); } if(isset($_POST['custom_checkbox'])){ $custom_checkbox_value = wp_filter_nohtml_kses($_POST['custom_checkbox']); add_comment_meta($comment_id, 'custom_checkbox', $custom_checkbox_value); } } add_action('comment_post', 'save_custom_comment_fields');
代码语言:txt
复制
 function display_custom_comment_fields($comment_text, $comment){
代码语言:txt
复制
     $custom_field_value = get_comment_meta($comment->comment_ID, 'custom_field', true);
代码语言:txt
复制
     $custom_checkbox_value = get_comment_meta($comment->comment_ID, 'custom_checkbox', true);
代码语言:txt
复制
     if($custom_field_value){
代码语言:txt
复制
         $comment_text .= '<p><strong>自定义字段:</strong> ' . $custom_field_value . '</p>';
代码语言:txt
复制
     }
代码语言:txt
复制
     if($custom_checkbox_value){
代码语言:txt
复制
         $comment_text .= '<p><strong>复选框:</strong> ' . $custom_checkbox_value . '</p>';
代码语言:txt
复制
     }
代码语言:txt
复制
     return $comment_text;
代码语言:txt
复制
 }
代码语言:txt
复制
 add_filter('comment_text', 'display_custom_comment_fields', 10, 2);
代码语言:txt
复制
 ```
  • 这将保存自定义字段/复选框的值,并在评论显示时将其添加到评论内容中。

通过以上步骤,你可以成功地将自定义字段/复选框添加到WordPress注释中。这样,用户在发表评论时就可以提供额外的信息或选择。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

  • 领券