0

I am trying to modify the comment displayed in the comment list and for that i am using the following code:

add_filter( 'comment_text', 'modify_comment');
function modify_comment( $text ){
wp_enqueue_script('cmt-player', plugins_url( 'js/cmt-player.js',__FILE__ ), array( 'jquery' ), '', true );
$comment_id = get_comment_ID();
$file_url = get_comment_meta($comment_id, 'record_file', true );
$site_parameters = array(
            'file_url' => $file_url,
            'plugin_url' => plugins_url(),
            'theme_directory' => get_template_directory_uri(),
            'comment_id' => $comment_id,
        ); 
wp_localize_script( 'cmt-player', 'wpvr_cmtvar', $site_parameters );
require( 'lib/cmtplayer-interface.php' );
}

But i am not able to get the comment id for a particular comment in the comment list, it is getting the comment id for the new comment only..

Any help would be appreciated!!

1

The comment_text filter passes the comment as one of its params:

echo apply_filters( 'comment_text', $comment_text, $comment, $args );

So you don't need to call get_comment_ID(), you can just access the ID from the $comment object.

Change:

add_filter( 'comment_text', 'modify_comment');

to:

add_filter( 'comment_text', 'modify_comment', 0, 3 );

And then:

function modify_comment( $comment_text, $comment, $args ){
    wp_enqueue_script( 'cmt-player', plugins_url( 'js/cmt-player.js',__FILE__ ), array( 'jquery' ), '', true );
    $comment_id = absint( $comment->comment_ID );
    $file_url = get_comment_meta( $comment_id, 'record_file', true );
    $site_parameters = array(
        'file_url'        => $file_url,
        'plugin_url'      => plugins_url(),
        'theme_directory' => get_template_directory_uri(),
        'comment_id'      => $comment_id,
    ); 
    wp_localize_script( 'cmt-player', 'wpvr_cmtvar', $site_parameters );
    require( 'lib/cmtplayer-interface.php' );
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.