Wordpress添加注释列并保存自定义数据


Wordpress Add Comment Column and Save custom data

在最近的一个项目中,客户需要在评论管理区输入<select />。其想法是,网站所有者逐一阅读每条评论,并需要记住女巫评论对他来说很重要,因此<select />将有两个输入"感兴趣"-"不感兴趣"。

在wordpress评论管理上创建一个自定义栏,由于wordpress Codex ,这并没有带来太大的痛苦

/* Display custom column */
function display_posts_stickiness( $column, $post_id ) {
    echo '<form method="post">';
    echo '<input type="hidden" name="populate_importancy" value="1" />';
    echo '<select name="importanceList[]">';
    echo '<option value="1">Please select</option>';
    echo '<option value="2">Importnat</option>';
    echo '<option value="3">No important</option>';
    echo '</select>';
    echo '<input type="submit" id="submit" value="Choose" />';
    echo '</form>';
}
add_action( 'manage_comments_custom_column' , 'display_posts_stickiness', 10, 2 );
 /* Add custom column to post list */
 function add_sticky_column( $columns ) {
     return array_merge( $columns, 
         array( 'sticky' => __( 'Comment Importance', 'importancy_com_txt' ) ) );
 }
 add_filter( 'manage_edit-comments_columns' , 'add_sticky_column' );

在此之前,所有的工作都像一个魅力,列和下拉列表已经在新的评论管理列上显示。

我的主要问题从这里开始,当我不想在数据库上保存用户选项时。

add_action( 'save_post', 'save_importancy' );
function save_importancy( $comment_id ) {
    foreach($_POST["importanceList"] as $s) {  
        //'importancy' column have been added under cns_comments table
        $insertSQL = sprintf("INSERT INTO cns_comments (importancy) VALUES(LAST_INSERT_ID(),". $s . ")"); 
    }  
}

我不是一个PHP开发人员,我的PHP知识目前还很有限,在我把问题发布到这里之前,我已经讨论了几天了。

我真的很感激在这件事上能给我一点帮助,谢谢你。

$insertSQL = sprintf("INSERT INTO cns_comments (id, importancy) VALUES(LAST_INSERT_ID(),'%s')", $s); 

您的sql语法和sprintf的使用是不正确的。您试图写入2个字段,但只指定了重要性。假设id是最后一个插入id的字段,那么上面的内容就可以了。此外,您实际上需要运行查询。既然你在使用wordpress,你应该只使用他们的内部数据库类:

function save_importancy( $comment_id ) {
    global $wpdb;
    foreach($_POST["importanceList"] as $s) {  
        //'importancy' column have been added under cns_comments table
        $insertSQL = $wpdb->prepare("INSERT INTO cns_comments (id, importancy) VALUES (LAST_INSERT_ID(), %s)", $s);
        $wpdb->query($insertSQL);
    }  
}

http://codex.wordpress.org/Class_Reference/wpdb

http://php.net/sprintf