-
-
Save bradyvercher/6e8c6aed8188ddabf612 to your computer and use it in GitHub Desktop.
Blazer Six Blog: Comment Preview Post (http://www.blazersix.com/blog/treasure-hunting-in-wordpress-core/)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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