Skip to content

Instantly share code, notes, and snippets.

@bradyvercher
Last active October 10, 2015 16:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bradyvercher/6e8c6aed8188ddabf612 to your computer and use it in GitHub Desktop.
Save bradyvercher/6e8c6aed8188ddabf612 to your computer and use it in GitHub Desktop.
<script type="text/javascript">
jQuery(function( $ ) {
$( '#comment-preview-button' ).on( 'click', function( e ) {
e.preventDefault();
// The fields won't be present for logged in users.
var commentAuthor = commentForm.find( 'input[name="author"]' ),
commentAuthorEmail = commentForm.find(' input[name="email"]' ),
commentAuthorUrl = commentForm.find( 'input[name="url"]' );
// Do client side validation of comment data before performing the AJAX request.
$.ajax({
url : 'wp-comments-post.php',
type : 'POST',
data : {
mode : 'preview',
author : commentAuthor.length ? commentAuthor.val() : '',
email : commentAuthorEmail.length ? commentAuthorEmail.val() : '',
url : commentAuthorUrl.lenght ? commentAuthorUrl.val() : '',
comment : commentForm.find( 'textarea[name="comment"]' ).val(),
comment_post_ID : commentForm.find( 'input[name="comment_post_ID"]' ).val()
},
dataType : 'json',
success : function( data ) {
// Insert the comment HTML.
}
});
});
});
</script>
<?php
/**
* Filter requests for comment previews and return JSON.
*
* @param mixed $approved Approval status (0|1|'spam').
* @param array $commentdata Comment information.
* @return mixed Prints comment data if context is preview, otherwise returns approval status.
*/
function blazersix_comment_preview( $approve, $commentdata ) {
// Check to make sure the context is 'preview'.
if ( isset( $_POST['mode'] ) && 'preview' == $_POST['mode'] ) {
// Retrieve custom HTML here.
$commentdata['html'] = blazersix_get_comment_html( $commentdata );
// Short-circuit the request and return comment data.
echo json_encode( $commentdata );
wp_die();
}
return $approve;
}
add_filter( 'pre_comment_approved', 'blazersix_comment_preview', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment