Help with new functions.php code Delete Comments Front End

I am trying to add a function to delete the comments (messages) from the front end.

This is a WordPress theme that uses the commenting ability to allow Users (Subscribers) to message each other. But there is no Delete function. Front end Users (Subscribers) can not delete messages.

There are 2 functions.php files. 1st in root/functions.php 2nd in root/framework/functions.php

1st in root/functions.php: I have not added additional code

//Define constants
define('SITE_URL', home_url().'/');
define('AJAX_URL', admin_url('admin-ajax.php'));
define('THEME_PATH', get_template_directory().'/');
define('CHILD_PATH', get_stylesheet_directory().'/');
define('THEMEX_PREFIX', 'themex_');

2nd in root/framework/functions.php Original code sample

function themex_get_string($key, $type, $default) {
    $name=$key.'-'.$type;
    $string=$default;   
    $strings=array();
    include(THEMEX_PATH.'strings.php');

    if(isset($strings[$name])) {
        $string=$strings[$name];
    }

    return themex_stripslashes($string);
}

Code I added to 2nd in root/framework/functions.php Does Not Work! Creates a Server Error!

function custom_delete_post_comment() {
   $comment_id = comment_ID();
   wp_delete_comment( $comment_id, true ) 
}
if ( isset( $_POST['comment_delete_nonce'] ) ) {
if( wp_verify_nonce( $_POST['comment_remove_nonce'], 'comment-remove-nonce' ) ) {
set_query_var( 'commentid1', $_POST['commentid'] );     
wp_delete_comment( get_query_var( 'commentid1'), true ); 
wp_safe_redirect( wp_get_referer() ); exit;     
}

Could someone please look at my functions code and help me to code it correctly please.

Additional code that I have added to the template-messages.php between <form method .....</form>

<li>
       <div class="listed-message">
           <header class="message-header clearfix">
               <h6 class="message-author"><a href="<?php echo get_author_posts_url($comment->user_id); ?>"><?php comment_author(); ?></a></h6>
            <time class="message-date" datetime="<?php comment_time('Y-m-d'); ?>"><?php comment_time(get_option('date_format')); ?></time>
        </header>
        <?php comment_text(); ?>
    </div>

<form method="POST" action="" class="delete-comment-form">
<input type="hidden" name="comment_remove_nonce" value="<?php echo wp_create_nonce('comment-remove-nonce'); ?>" /> 

<input type="hidden" name="commentid" value="<?php comment_ID() ?>" />

<input type="submit" value="Delete" title="Delete" id="submit-btn" class="btn" />
</form>

</li>

I now have this functions code:

function custom_delete_post_comment() {
   $comment_id = comment_ID();
   wp_delete_comment( $comment_id, true ) ;
}
if ( isset( $_POST['comment_delete_nonce'] ) ) {
    if( wp_verify_nonce( $_POST['comment_remove_nonce'], 'comment-remove-nonce' ) ) {
    set_query_var( 'commentid1', $_POST['commentid'] );     
    wp_delete_comment( get_query_var( 'commentid1'), true ); 
    wp_safe_redirect( wp_get_referer() ); exit;        
    }
} 

and this form code is added to create a Delete button:

<form method="POST" action="" class="delete-comment-form">
<input type="hidden" name="comment_remove_nonce" value="<?php echo wp_create_nonce('comment-remove-nonce'); ?>" /> 

<input type="hidden" name="commentid" value="<?php comment_ID() ?>" />

<input type="submit" value="Delete" title="Delete" id="submit-btn" class="btn" />
</form>

Yet when I click the Delete button the page reloads, yet the comment/message is still there?

All help appreciated :slight_smile:

Is there a confusion between delete and remove in the code? Your form has this

<input type="hidden" name="comment_remove_nonce" value="<?php echo wp_create_nonce('comment-remove-nonce'); ?>" /> 

which creates a field that will come over as $_POST['comment_remove_nonce'], but you check for:

if ( isset( $_POST['comment_delete_nonce'] ) ) {
1 Like

Hello droopsnoot, thank you for your reply and observation, I went and changed the code to that below, yet no joy, still no deletion of the comment/message.

<form method="POST" action="" class="delete-comment-form">

<input type="hidden" name="comment_delete_nonce" value="<?php echo wp_create_nonce('comment_delete_nonce'); ?>" /> 

<input type="hidden" name="commentid" value="<?php comment_ID() ?>" />

<input type="submit" value="Delete" title="Delete" id="submit-btn" class="btn" />
</form>

and in functions.php

function custom_delete_post_comment() {
       $comment_id = comment_ID();
       wp_delete_comment( $comment_id, true ) ;
    }
 if ( isset( $_POST['comment_delete_nonce'] ) ) {
    if( wp_verify_nonce( $_POST['comment_remove_nonce'], 'comment-remove-nonce' ) ) {
    set_query_var( 'commentid1', $_POST['commentid'] );     
    wp_delete_comment( get_query_var( 'commentid1'), true ); 
    wp_safe_redirect( wp_get_referer() ); exit;     
    }
}

So I thought I would try a New coding method, new code below:

added to functions.php

add_action( 'wp_ajax_my_delete_comment', 'my_delete_comment' );
    function my_delete_comment(){
    $permission = check_ajax_referer( 'my_delete_comment_nonce', 'nonce', false );
      if( $permission == false ) {  
         echo 'error';
      }
      else {    
            wp_delete_comment( $_REQUEST['id'], true );  
            echo 'success';
           }
     die();
    }

added to config.php

//comment delete
            array(  
                'name' => 'my_delete_comment',
                'uri' => THEME_URI.'js/jquery.my_delete_comment.js',
            ),

created a js file: jquery.my_delete_comment

jQuery( document ).ready( function($) {
$(document).on( 'click', '.comment-delete', function() {
    var r = confirm("Delete This Comment?");
    if (r == true) {
    var id = $(this).data('id');
    var nonce = $(this).data('nonce');
    var post = $(this).parents('.post:first');
    var space = (" ");
    $.ajax({
        type: 'post',
        url: MyAjax.ajaxurl,
        data: {
            action: 'my_delete_comment',
            nonce: nonce,
            id: id
        },
        success: function( result ) {
            if( result == 'success' ) {
                $('#div-comment-' + id).fadeOut('slow');
            }
        }
    })
    }
    return false;
  })
 })

and the button code: form code added below message code

<li>
    <div class="listed-message">
        <header class="message-header clearfix">
            <h6 class="message-author"><a href="<?php echo get_author_posts_url($comment->user_id); ?>"><?php comment_author(); ?></a></h6>
            <time class="message-date" datetime="<?php comment_time('Y-m-d'); ?>"><?php comment_time(get_option('date_format')); ?></time>
        </header>
        <?php comment_text(); ?>
    </div>

<form method="POST" action="" class="delete-comment-form">
<input type="hidden" name="my_delete_comment_nonce" value="<?php echo wp_create_nonce('my_delete_comment_nonce'); ?>" /> 
  <a href="#" data-id="<?php comment_ID() ?>" data-nonce="<?php echo wp_create_nonce('my_delete_comment_nonce') ?>" class="btn"></a>
<input type="hidden" name="commentid" value="<?php comment_ID() ?>" />

<input type="submit" value="Delete" title="Delete" id="submit-btn" class="btn" />
</form>

</li>

But still no Joy. On click page reloads, yet no deletion of comment/message.

I can’t help with the jQuery stuff, I’ve tried a bit but not very much, and have no Wordpress experience (which is what I assume all the “wp_” stuff is about).

But in the code you put at the top, there are still inconsistencies. In your form, you have:

<input type="hidden" name="comment_delete_nonce" value="<?php echo wp_create_nonce('comment_delete_nonce'); ?>" /> 

But then in functions.php you have:

 if ( isset( $_POST['comment_delete_nonce'] ) ) {
    if( wp_verify_nonce( $_POST['comment_remove_nonce'], 'comment-remove-nonce' ) ) {

So where is $_POST['comment_remove_nonce'] in the second line of code coming from? The form doesn’t have a field called that, it’s called $_POST['comment_delete_nonce'], as you use in the first line.

1 Like

Hello droopsnoot, Yes Yes Yes it is now working! Deleting messages (comments) correctly. Many thank yous for reminding me again about the “still inconsistencies. In your form”

A strange messaging system, as when the receiver deletes a message the message also is deleted from the senders messages. That’s comments delete …lol

I also had to make a change to the functions code, removed the last line, as it was preventing the page from reloading.

Checked the database and message are been deleted from the database.

Thank you again droopsnoot :slight_smile:

Here is the working code:

function custom_delete_post_comment() {
       $comment_id = comment_ID();
       wp_delete_comment( $comment_id, true ) ;
    }
 if ( isset( $_POST['comment_delete_nonce'] ) ) {
    if( wp_verify_nonce( $_POST['comment_delete_nonce'], 'comment_delete_nonce' ) ) {
    set_query_var( 'commentid1', $_POST['commentid'] );     
    wp_delete_comment( get_query_var( 'commentid1'), true );      
    }
}

<form method="POST" action="" class="delete-comment-form">
<input type="hidden" name="comment_delete_nonce" value="<?php echo wp_create_nonce('comment_delete_nonce'); ?>" /> 
<input type="hidden" name="commentid" value="<?php comment_ID() ?>" />
<input type="submit" value="Delete" title="Delete" id="submit-btn" class="btn" />
</form>
1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.