$commentData['comment_post_ID'], 'comment_author_IP' => $commentData['comment_author_IP'], 'comment_author_url' => $commentData['comment_author_url'], 'comment_author' => $commentData['comment_author'], 'comment_author_email' => $commentData['comment_author_email'], 'comment_content' => $commentData['comment'], 'comment_type' => 'comment', 'comment_meta' => array( 'phone' => $commentData['comment_author_phone'], // 将自定义字段添加到评论元数据中 'compname' => $commentData['comment_compname'], // 将自定义字段添加到评论元数据中 'address' => $commentData['comment_provinces'].$commentData['comment_city'].$commentData['comment_area'].' '.$commentData['comment_address'], // 将自定义字段添加到评论元数据中 ) ); // 插入评论 $comment_id = wp_insert_comment($comment_args); if ($comment_id !== 0) { echo json_encode(['status' => 'success', 'comment_id' => $comment_id]); } else { echo json_encode(['status' => 'error', 'message' => 'Failed to submit comment.']); } } die(); // 必须终止脚本 } add_action('wp_ajax_submit_custom_comment', 'submit_custom_comment'); // 钩子 - 处理登录用户的请求 add_action('wp_ajax_nopriv_submit_custom_comment', 'submit_custom_comment'); // 钩子 - 处理非登录用户的请求 // 添加后台评论处显示自定义字段代码 add_filter( 'manage_edit-comments_columns', 'my_comments_columns' ); add_action( 'manage_comments_custom_column', 'output_my_comments_columns', 10, 3 ); function my_comments_columns( $columns ){ $columns[ 'phone' ] = __( '电话' ); $columns[ 'compname' ] = __( '主营项目' ); $columns[ 'address' ] = __( '地址' ); return $columns; } function output_my_comments_columns( $column_name, $comment_id ){ switch( $column_name ) { case "phone" : echo get_comment_meta( $comment_id, 'phone', true ); break; case "compname" : echo get_comment_meta( $comment_id, 'compname', true ); break; case "address" : echo get_comment_meta( $comment_id, 'address', true ); break; } } // 在文章被创建时设置初始浏览计数 add_action('draft_to_publish', 'set_post_views_on_publish'); add_action('save_post', 'set_post_views_on_publish'); function set_post_views_on_publish($post_ID) { if (!isset($_POST['post_ID'])) { return; } $post_ID = $_POST['post_ID']; if (!isset($_POST['post_status']) || 'publish' !== $_POST['post_status']) { return; } $count_key = 'views'; $count = (int) get_post_meta($post_ID, $count_key, true); if (!$count) { delete_post_meta($post_ID, $count_key); add_post_meta($post_ID, $count_key, '0'); } } // 添加封面图片选项到文章编辑页面 function add_featured_image_field_to_edit_page() { add_meta_box('featured_image_meta_box', '设置封面图片', 'render_featured_image_meta_box', 'post', 'normal', 'high'); } add_action('add_meta_boxes', 'add_featured_image_field_to_edit_page'); // 渲染封面图片选项界面 function render_featured_image_meta_box($post) { // 非必要代码,用于在编辑页面显示提示信息 echo '
请上传作为文章封面的图片:
'; // 创建图片上传表单 wp_nonce_field('featured_image_nonce', 'featured_image_nonce_field'); ?> max_num_pages; } if( $max_page >1 ) { echo "