On a recent project customer need to have a <select />
input on comments admin area. The thought is, that the website owner read every comment one by one, and need to remember witch comment is important for him, so the <select />
will have two inputs “Interested” – “No Interested”.
Creating a custom colummn on wordpress comments admin, it wasn’t to much of pain due to the 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' );
Until there all working like a charm, the column and dropdown have been apeared on the new comment admin column.
The main problem for me starts here, when i wan’t to save the user option on the database.
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 . ")");
}
}
I am not a PHP developer and my PHP knowledge is limited as for now, and have been around this for a couple of days before I post my problem here.
I would really appreciate a bit of help in this matter, thank you.
your sql syntax and use of sprintf is incorrect. You are trying to write 2 fields but you only specify importancy. Assuming id is the field for last insert id, the above would work.
Also, you actually need to run the query. Since you’re using wordpress, you should just use their internal db class:
http://codex.wordpress.org/Class_Reference/wpdb
http://php.net/sprintf