BuddyDev

Search

[Resolved] Change comment edit link/button to "delete" button

  • Participant
    Level: Master
    Posts: 285
    evillizard on #22449

    Hello, good day

    Please can you give me a code to make this “EDIT” button/link to be a “DELETE” button

    Look at pic to see the “edit” button : https://postimg.cc/R30zwG4D

    There are some moderator users that i want them to be able to only DELETE comments.. And not be able to change the comments… To avoid them changing or tampering with peoples comments

    I just want them to be able to see and delete the comment thats all.. Nothing else

  • Keymaster
    (BuddyDev Team)
    Posts: 24623
    Brajesh Singh on #22450

    Hi,
    You should try this
    https://wordpress.org/plugins/simple-comment-editing/

    Regards
    Brajesh

  • Participant
    Level: Master
    Posts: 285
    evillizard on #22467

    Nope.. I’ve tried that plugin before, it doesn’t solve the problem

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3115
    Ravi on #22478

    Hi,

    How do we know user is moderator?.

    Regards
    Ravi

  • Participant
    Level: Master
    Posts: 285
    evillizard on #22499

    Moderator is just a user role i created… Pay no attention to it.. Its just the user role i created and gave it the “cap.ability” to edit posts.. So normally that edit link would show to anyone with that role

    ..

    My problem is:-

    If the moderator should click this “edit” link: https://postimg.cc/q6k0PbGf

    they would be taking here : https://postimg.cc/5HqRhxK0 .. To edit the comment. (OF WHICH THEY CAN CHEAT BY CHANGING THE COMMENTS)

    ….

    I only want them to be able to delete the comment: https://postimg.cc/k2YbZssB

    and not to edit it.

    Imagine if you host a riddle or quiz and ask : what is the capital of india

    And you want the moderators to only delete wrong answers and leave right answers

    -with the way it is..some moderators cld cheat for some people by changing their comments ..

    Thats what im trying to avoid

    (if there is another way around this problem, im open to it.)

    .

    Also: an alert before the moderator deletes comment would be nice.. Like maybe something like “ARE YOU SURE YOU WANT TO DELETE THIS COMMENT?” to avoid “mistake-deleting”

  • Participant
    Level: Master
    Posts: 285
    evillizard on #22509

    Because i use this code in wp-config.php

    define('EMPTY_TRASH_DAYS', 0);

    To stop the comments from moving to “BIN”

    • This reply was modified 5 years, 7 months ago by evillizard.
  • Participant
    Level: Master
    Posts: 285
    evillizard on #22521
  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3115
    Ravi on #22528

    Hello,

    For delete comment use the following code.

    
    function buddydev_delete_comment() {
    
    	if ( ! isset( $_GET['action'] ) || ! isset( $_GET['id'] ) || 'delete_comment' != $_GET['action'] ) {
    		return;
    	}
    
    	if ( ! isset( $_GET['nonce'] ) || ! wp_verify_nonce( $_GET['nonce'], 'delete-comment' ) ) {
    		return;
    	}
    
    	$comment = get_comment( $_GET['id'] );
    
    	if ( empty( $comment ) || $comment->user_id != get_current_user_id() ) {
    		return;
    	}
    
    	wp_delete_comment( $comment->comment_ID );
    
    	wp_safe_redirect( get_permalink( $comment->comment_post_ID ) );
    	exit;
    }
    
    add_action( 'template_redirect', 'buddydev_delete_comment' );
    
    function buddydev_add_delete_comment_button() {
    
    	if ( ! is_user_logged_in() ) {
    		return;
    	}
    
    	$comment = get_comment();
    
    	if ( $comment->user_id != get_current_user_id() ) {
    		return;
    	}
    
    	$delete_url = add_query_arg( array(
    		'id' => $comment->comment_ID,
    		'action' => 'delete_comment',
    		'nonce' => wp_create_nonce( 'delete-comment' ),
    	), get_permalink( $comment->comment_post_ID ) );
    
    	echo sprintf( '<a href=%s>%s</a>', $delete_url, __( 'Delete' ) );
    }
    
    add_filter( 'wp_list_comments_args', function( $r ) {
    	$r['end-callback'] = 'buddydev_add_delete_comment_button';
    	return $r;
    } );
    
    

    Please check

    Regards
    Ravi

  • Participant
    Level: Master
    Posts: 285
    evillizard on #22533

    Yes like that.. But that code gives EVERY USER the capability to DELETE THEIR OWN COMMENTS..

    1: (i don’t want all the users to be able to delete their own comments.. I want ONLY jst a specific user-role to be able to delete anyones & everyones comments.. Just like you did for the “REPLY” button on this post: https://buddydev.com/support/forums/topic/enable-thread-nested-comments-or-comment-reply-button-for-specific-user-role/#post-22177

    You used the contributor & author roles for example… Please apply the same in this case, Just Write the code so it be only the “CONTRIBUTOR” user-role that is able to delete EVERYONE’S comment that way.. and no one else apart from users with “contributor-roles” should be able to see that delete button/link..

    ….


    2:

    And please can there be a confirmation message before the comment is deleted, Something like : “ARE YOU SURE YOU WANT TO DELETE THIS COMMENT” ?

    This might help with it: https://wordpress.stackexchange.com/questions/271686/add-confirmation-popup-on-move-to-trash

    Or this:
    https://stackoverflow.com/questions/9139075/how-to-show-a-confirm-message-before-delete

    • This reply was modified 5 years, 7 months ago by evillizard.
    • This reply was modified 5 years, 7 months ago by evillizard.
  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3115
    Ravi on #22537

    Hello,

    I have modified the code now you can allow certain roles who can delete comments. I am sorry but due to lack of time, I am unable to assist you with confirm message.

    
    function buddydev_delete_comment() {
    
    	if ( ! isset( $_GET['action'] ) || ! isset( $_GET['id'] ) || 'delete_comment' != $_GET['action'] ) {
    		return;
    	}
    
    	if ( ! isset( $_GET['nonce'] ) || ! wp_verify_nonce( $_GET['nonce'], 'delete-comment' ) ) {
    		return;
    	}
    
    	$comment = get_comment( $_GET['id'] );
    
    	if ( empty( $comment ) || ! buddydev_user_can_delete( get_current_user_id() ) ) {
    		return;
    	}
    
    	wp_delete_comment( $comment->comment_ID );
    
    	wp_safe_redirect( get_permalink( $comment->comment_post_ID ) );
    	exit;
    }
    
    add_action( 'template_redirect', 'buddydev_delete_comment' );
    
    function buddydev_add_delete_comment_button() {
    
    	if ( ! is_user_logged_in() || ! buddydev_user_can_delete( get_current_user_id() ) ) {
    		return;
    	}
    
    	$comment = get_comment();
    
    	$delete_url = add_query_arg( array(
    		'id' => $comment->comment_ID,
    		'action' => 'delete_comment',
    		'nonce' => wp_create_nonce( 'delete-comment' ),
    	), get_permalink( $comment->comment_post_ID ) );
    
    	echo sprintf( '<a href=%s>%s</a>', $delete_url, __( 'Delete' ) );
    }
    
    add_filter( 'wp_list_comments_args', function( $r ) {
    	$r['end-callback'] = 'buddydev_add_delete_comment_button';
    	return $r;
    } );
    
    function buddydev_user_can_delete( $user_id ) {
    
    	if ( ! is_user_logged_in() || empty( $user_id ) ) {
    		return false;
    	}
    
    	// Add roles who can delete comments
    	$allowed_roles = array( 'administrator', 'editor' );
    
    	$user_id = $user_id ? $user_id : get_current_user_id();
    
    	$user = get_user_by( 'id', $user_id );
    
    	$common_roles = array_intersect( $allowed_roles, $user->roles );
    
    	if ( empty( $common_roles ) ) {
    		return false;
    	}
    
    	return true;
    }
    
    

    Regards
    Ravi

    • This reply was modified 5 years, 7 months ago by Ravi.

The topic ‘ [Resolved] Change comment edit link/button to "delete" button’ is closed to new replies.

This topic is: resolved