Skip to content

Instantly share code, notes, and snippets.

@5ally
Created July 7, 2022 15:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 5ally/88320109c9bb534d89e47231cf500c98 to your computer and use it in GitHub Desktop.
Save 5ally/88320109c9bb534d89e47231cf500c98 to your computer and use it in GitHub Desktop.
Custom comment's AJAX actions (front-end), tested working with WordPress v6.0 - See https://wordpress.stackexchange.com/a/407061/137402 for details
jQuery( document ).ready( function ( $ ) {
// Use '' to disable the delete's confirmation; and do that at your own risks..
var confirmDeleteText = 'Are you sure you want to permanently delete this comment??';
// Removes the spam/trashed/deleted comment's element from the DOM. Make sure
// the selectors like LI.comment and .comment-body exist in your comments
// list.
function removeComment( link ) {
var $comment = $( link ).closest( 'li.comment' );
var $children = $comment.find( 'li.comment' );
var $parent = $comment.closest( 'li.parent' );
var $el = $comment;
if ( $children.length >= 1 ) {
$el = $( link ).closest( '.comment-body' );
}
// Fade-out the comment or comment body element upon removing it. Just
// replace the background-color's value to whatever you like, or if
// you don't want that color effect, then remove it.
$el.css( 'background-color', '#CCEEBB' ).fadeOut( 350, function () {
$( this ).remove();
// If the parent element no longer contains any comment elements,
// then the parent element will be deleted.
if ( $parent.find( 'li.comment' ).length < 1 ) {
$parent.remove();
}
} );
}
// Updates a comment's status, or delete the comment, via the REST API.
function commentApiRequest( link ) {
var action = $( link ).data( 'action' );
var comment_id = $( link ).data( 'id' );
var isDeleting = ( 'delete' === action );
if ( confirmDeleteText && isDeleting && ! confirm( confirmDeleteText ) ) {
return false;
}
wp.apiRequest( {
path: 'wp/v2/comments/' + comment_id,
method: isDeleting ? 'DELETE' : 'POST',
data: isDeleting ? { force: true } : { status: action }
} ).done( function () {
removeComment( link );
} );
}
// Add the AJAX action links (Spam, Trash and Delete) next to the comment's
// edit link. Just change the HTML to your own liking.
$( '.comment-edit-link', 'li.comment' ).after( function () {
var matches = this.href.match( /[\?&]c=(\d+)/ );
var comment_id = matches[1] || 0;
return ' | <a href="#" class="comment-ajax-action" data-id="' + comment_id + '" data-action="spam">Spam</a>' +
' | <a href="#" class="comment-ajax-action" data-id="' + comment_id + '" data-action="trash">Trash</a>' +
' | <a href="#" class="comment-ajax-action" data-id="' + comment_id + '" data-action="delete">Delete</a>';
} );
// If you changed the action link's class (default: comment-ajax-action),
// then be sure to also change the one below!
$( '.comment-ajax-action' ).on( 'click', function ( e ) {
e.preventDefault();
commentApiRequest( this );
} );
} );
<?php
add_action( 'wp_enqueue_scripts', 'my_theme_scripts' );
function my_theme_scripts() {
// Do nothing if we're not on the single post page, or if the post is
// password-protected and a correct password has not been provided, or
// if the current user is not allowed to moderate comments.
if ( ! is_singular() || post_password_required( get_queried_object() ) ||
! current_user_can( 'moderate_comments' )
) {
return;
}
wp_enqueue_script(
'your-theme-comment-ajax-actions',
get_template_directory_uri() . '/assets/js/comment-ajax-actions.js',
array( 'jquery', 'wp-api-request' ),
wp_get_theme()->get( 'Version' ),
true
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment