【问题标题】:Can't edit comment meta data in Wordpress无法在 Wordpress 中编辑评论元数据
【发布时间】:2016-08-10 07:04:48
【问题描述】:

我使用的是 Wordpress(4.5.3 版),并在评论表单中添加了一个自定义字段。该字段是使用选择标签的下拉列表。该字段在表单中显示得很好,我可以选择一个选项,该选项的值会正确保存。

我还添加了代码以在编辑评论页面 (/wordpress/wp-admin/comment.php?action=editcomment) 中编辑自定义字段。我能够检索存储的值,然后使用选择的正确值重新创建下拉列表。

我添加了代码来保存自定义字段的编辑值,但这不起作用。当我从“编辑评论”页面从下拉列表中选择不同的值,然后单击“更新”按钮时,新选择的值不会被保存。

我在functions.php 中添加代码来完成所有这些工作。这是将字段添加到表单并存储数据的代码:

// Add fields after default fields above the comment box, always visible

    add_action( 'comment_form_logged_in_after', 'additional_fields' );
    add_action( 'comment_form_after_fields', 'additional_fields' );

    function additional_fields () {

    echo '<p class="comment-form-area">'.
    '<label for="region">' . __( 'Choose a <strong>region</strong>' ) . '<span class="required">*&nbsp;&nbsp;&nbsp;&nbsp;</span></label>'.          
    '<br /><select id="region" name="region">
    <option value="one">one</option>
    <option value="two">two</option>
    <option value="three">three</option>
    <option value="four">four</option>      
    </select></p>';
    }

    // Save the comment meta data along with comment

    add_action( 'comment_post', 'save_comment_meta_data' );
    function save_comment_meta_data( $comment_id ) {

        if ( ( isset( $_POST['region'] ) ) && ( $_POST['region'] != '') )
        $region = wp_filter_nohtml_kses($_POST['region']);              
        add_comment_meta( $comment_id, 'region', $region );     

    }

以下是将自定义字段添加到评论编辑页面的代码:

// Add an edit option to comment editing screen  

    add_action( 'add_meta_boxes_comment', 'extend_comment_add_meta_box' );

    function extend_comment_add_meta_box() {
        add_meta_box( 'title', __( 'Region field' ),                'extend_comment_meta_box', 'comment', 'normal', 'high' );
    }

    function extend_comment_meta_box ( $comment ) {

    $_regions = array
        ("one",
        "two",
        "three",
        "four");


        $region = get_comment_meta( $comment->comment_ID, 'region', true );         
        ?>
        <p>
            <label for="region"><?php _e( 'Region*' ); ?></label>
            <p><select id="region" name="region">
        <?php
        for ($ix = 0; $ix < count($_regions); $ix++) {
            echo '<option value="' . $_regions[$ix] . '"';
            if ($region == $_regions[$ix]) {
                echo ' selected';                   
            }
        echo '>' . $_regions[$ix] . '</option>';    
        }
        ?>
        </select></p>

    <?php
    }

所有这些似乎都运行良好。以下是保存自定义字段的编辑值的代码:

// Update comment meta data from comment editing screen 

    add_action( 'edit_comment', 'extend_comment_edit_metafields' );

    function extend_comment_edit_metafields( $comment_id ) {
        if( ! isset( $_POST['extend_comment_update'] ) || ! wp_verify_nonce(        $_POST['extend_comment_update'], 'extend_comment_update' ) ) return;

        if ( ( isset( $_POST['region'] ) ) && ( $_POST['region'] != '') ) :
            $region = wp_filter_nohtml_kses($_POST['region']);
            update_comment_meta( $comment_id, 'region', $region );
        else :
            delete_comment_meta( $comment_id, 'region');
        endif;

    }

此代码不起作用。当我返回“编辑评论”页面时,我看到的是自定义字段的原始值,而不是编辑后的值。

为什么编辑的值没有被保存?

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    函数extend_comment_edit_metafields() 最初测试是否已设置nonce 字段。它没有因此返回执行而不调用 update_comment_meta()。为了正常工作,应该在 extend_comment_meta_box() 中设置 nonce 字段,如下所示:

    wp_nonce_field( 'extend_comment_update', 'extend_comment_update', false );
    

    nonce 字段的讨论在这里:https://codex.wordpress.org/Function_Reference/wp_nonce_field

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-10
      • 1970-01-01
      • 2022-07-05
      • 2022-07-13
      • 1970-01-01
      相关资源
      最近更新 更多