Skip to content

Instantly share code, notes, and snippets.

@dbranes
Last active November 3, 2017 12:56
Show Gist options
  • Save dbranes/0215762a4cf3db50a6b7 to your computer and use it in GitHub Desktop.
Save dbranes/0215762a4cf3db50a6b7 to your computer and use it in GitHub Desktop.
Multiple comment forms and lists in single.php - wpquestions.com #9749
/**
* Example how to setup up multiple comment forms and comments lists
*
* @version 0.0.5
* @file functions.php
* @see http://www.wpquestions.com/question/showLoggedIn/id/9749
*/
/**
* Count comments by type and approval
*
* @version 0.0.3
* @uses global $wpdb
* @param array $type
* @param int $post_id
* @param int $approved
* @return array $comments
*/
function wpq_get_comments_count( $types = array(), $post_id, $approved = 1 )
{
global $wpdb;
// Default
if ( ! is_array( $types ) || 0 == count( $types ) )
{
$types = array( '' );
}
// Construct the IN ( ... ) part
$tmp = array();
foreach( $types as $type )
{
$tmp[] = '%s';
}
$in = join( ',', $tmp );
// Use the vsprintf part of prepare:
$data = $types;
$data[] = $post_id;
$data[] = $approved;
$sql = $wpdb->prepare(
"SELECT COUNT(*) AS cc FROM {$wpdb->comments} WHERE comment_type IN ({$in}) AND comment_post_ID = %d AND comment_approved = %d ",
$data
);
return $wpdb->get_var( $sql );
}
/**
* Display a hidden input field to set the comment type as 'extra'
*
* @return void
*/
function wpq_hidden_input_field_to_set_extra_comment_content_type()
{
echo '<input name="wpq_comment_type" type="hidden" value="extra" />';
}
/**
* Filter normal comments
*
* @uses wpq_filter_comments()
* @param array $comments
* @return array $comments
*/
function wpq_show_only_normal_comments( $comments )
{
return wpq_filter_comments( array( '', 'pingback', 'trackback' ), $comments );
}
/**
* Filter comments of the type 'extra'
*
* @uses wpq_filter_comments()
* @param array $comments
* @return array $comments
*/
function wpq_show_only_extra_comments( $comments )
{
return wpq_filter_comments( array( 'extra' ), $comments );
}
/**
* Filter comments by comment type
*
* @uses wpq_filter_comments()
* @param string $comment_type
* @param array $comments
* @return array $comments
*/
function wpq_filter_comments( $comment_type, $comments )
{
$filtered_comments = array();
foreach( (array) $comments as $key => $comment )
{
if( in_array( $comment->comment_type, $comment_type, true ) )
{
$filtered_comments[] = $comment;
}
}
return $filtered_comments;
}
/**
* Change the default comment form settings
*
* @param array $defaults
* @return array $defaults
*/
function wpq_change_comment_form_defaults( $defaults )
{
$change_some_default_settings = array(
'comment_field' => '<p class="comment-form-comment"><label for="comment_extra">' . _x( 'Extra Comment', 'noun' ) . '</label> <textarea id="comment_extra" name="comment" cols="45" rows="8" aria-required="true"></textarea></p>',
'logged_in_as' => '',
'comment_notes_before' => '',
'comment_notes_after' => '',
'id_form' => 'commentform_extra',
'id_submit' => 'submit_extra',
'title_reply' => __( 'Leave an Extra Reply' ),
'title_reply_to' => __( 'Leave an Extra Reply to %s' ),
'cancel_reply_link' => __( 'Cancel Extra reply' ),
'label_submit' => __( 'Post an Extra Comment' ),
'format' => 'xhtml',
);
return wp_parse_args( $change_some_default_settings, $defaults );
}
/**
* Add the comment type to the comment data
*
* @uses wpq_preprocess_comment()
* @param array $commentdata
* @return array $commentdata
*/
function wpq_preprocess_comment_to_change_content_type( $commentdata )
{
$comment_type = filter_input( INPUT_POST, 'wpq_comment_type', FILTER_SANITIZE_STRING );
if( ! empty( $comment_type ) )
{
$commentdata['comment_type'] = $comment_type;
}
return $commentdata;
}
add_filter( 'preprocess_comment', 'wpq_preprocess_comment_to_change_content_type', 99 );
/**
* Example how to setup up multiple comment forms and comments lists
*
* @version 0.0.4
* @file single.php
* @see http://www.wpquestions.com/question/showLoggedIn/id/9749
*/
// -----------------------
// Normal Comment Form #1:
//
add_filter( 'comments_array', 'wpq_show_only_normal_comments' );
// Comments template:
// comments_template( 'normal-comments.php', true );
comments_template( '', true );
remove_filter( 'comments_array', 'wpq_show_only_normal_comments' );
// ----------------------
// Extra Comment Form #2:
//
add_action( 'comment_form', 'wpq_hidden_input_field_to_set_extra_comment_content_type' );
add_filter( 'comments_array', 'wpq_show_only_extra_comments' );
add_filter( 'comment_form_defaults', 'wpq_change_comment_form_defaults' );
// Comments template:
// comments_template( 'extra-comments.php', true );
comments_template( '', true );
remove_action( 'comment_form', 'wpq_hidden_input_field_to_set_extra_comment_content_type' );
remove_filter( 'comments_array', 'wpq_show_only_extra_comments' );
remove_filter( 'comment_form_defaults', 'wpq_change_comment_form_defaults' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment