You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Wpdiscuz保存评论元数据时$imageID无法传递问题求助

问题:评论元数据保存为NULL,无法传递图片ID到保存函数

我希望在发布评论时保存评论元数据,记录评论所属的图片ID,但当前代码保存的meta_value始终为NULL,$imageID值无法传递到save_comment_meta_data函数中。


用户提供的代码

网页代码

$imageID = 1;

functions.php代码

function my_wpdiscuz_shortcode() {
    $html = "";
    global $imageID;
    if (file_exists(ABSPATH . "wp-content/plugins/wpdiscuz/themes/default/comment-form.php")) {
        ob_start();
        include_once ABSPATH . "wp-content/plugins/wpdiscuz/themes/default/comment-form.php";
        $html = ob_get_clean();
    }
    return $html;
}
add_shortcode("wpdiscuz_comments", "my_wpdiscuz_shortcode");


function add_custom_comment_fields() {
    global $imageID;
    if ( isset( $imageID ) ) {
        echo '<input type="hidden" name="image_id" value="' . esc_attr( $imageID ) . '">';
    }
}
add_action( 'wpdiscuz_form_bottom', 'add_custom_comment_fields' );


function save_comment_meta_data( $comment_id ) {
    if ( isset( $_POST['image_id'] ) ) {
        $meta_value = sanitize_text_field( $_POST['image_id'] );
        add_comment_meta( $comment_id, 'image_id', $meta_value, true );
    }
}
add_action( 'comment_post', 'save_comment_meta_data' );

解决方案

问题根源有两个:

  1. 全局变量作用域不匹配:短码函数在WordPress钩子流程中执行,无法获取网页中直接定义的$imageID变量。
  2. HTML语法错误:隐藏输入框的闭合引号写错,导致value属性无法正确渲染。

修正步骤

1. 改用短码参数传递图片ID

放弃全局变量,通过短码参数直接传递图片ID,避免作用域问题:

  • 修改后的网页代码:
echo do_shortcode('[wpdiscuz_comments image_id="1"]');
  • 修改后的短码函数(functions.php):
function my_wpdiscuz_shortcode( $atts ) {
    $html = "";
    // 提取短码参数,设置默认值
    $atts = shortcode_atts( array(
        'image_id' => 0,
    ), $atts );
    
    // 将图片ID存入全局变量供后续钩子使用
    global $current_image_id;
    $current_image_id = $atts['image_id'];
    
    if (file_exists(ABSPATH . "wp-content/plugins/wpdiscuz/themes/default/comment-form.php")) {
        ob_start();
        include_once ABSPATH . "wp-content/plugins/wpdiscuz/themes/default/comment-form.php";
        $html = ob_get_clean();
    }
    return $html;
}
add_shortcode("wpdiscuz_comments", "my_wpdiscuz_shortcode");

2. 修正隐藏输入框的HTML输出

修复语法错误,确保输入框能正确渲染:

function add_custom_comment_fields() {
    global $current_image_id;
    if ( isset( $current_image_id ) && $current_image_id > 0 ) {
        echo '<input type="hidden" name="image_id" value="' . esc_attr( $current_image_id ) . '">';
    }
}
add_action( 'wpdiscuz_form_bottom', 'add_custom_comment_fields' );

3. 增强保存函数的安全性(可选)

添加合法性检查,避免非法请求,并支持元数据更新:

function save_comment_meta_data( $comment_id ) {
    // 验证是否为wpDiscuz的合法提交
    if ( ! isset( $_POST['wpdiscuz_comment_submit'] ) ) {
        return;
    }
    
    if ( isset( $_POST['image_id'] ) && is_numeric( $_POST['image_id'] ) ) {
        $meta_value = intval( sanitize_text_field( $_POST['image_id'] ) );
        // 存在则更新,不存在则添加
        if ( get_comment_meta( $comment_id, 'image_id', true ) ) {
            update_comment_meta( $comment_id, 'image_id', $meta_value );
        } else {
            add_comment_meta( $comment_id, 'image_id', $meta_value, true );
        }
    }
}
add_action( 'comment_post', 'save_comment_meta_data' );

内容的提问来源于stack exchange,提问作者jdbrown444

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 13:05:00