3

I have enabled comment replies in my blog. Lets say there is a comment Nick has written... when I'm gonna reply, I want to display a message "Replying to Nick" in the reply's form header. Anyone knows how? Thanks!

2

The WordPress function comment_form_title works only for users with Javascript disabled or pages without the comment-reply.js JavaScript loaded.

WordPress may not fix this limitation at all Two tickets have been opened before and closed without fix. http://core.trac.wordpress.org/ticket/10084 http://core.trac.wordpress.org/ticket/8639

However I managed to get it work for the default twentyeleven theme by using some dirty fixes. Here is the code you may use as plugin.

<?php
/**
 * Plugin Name: Comment Form Title Fix
 * Plugin URI: 
 * Description: WordPress provides comment_form_title function to displays text based on comment reply status. This only affects users with Javascript disabled or pages without the comment-reply.js JavaScript loaded. This plugin provides dirty fix to remove this limitation.
 * Author: tamilsweet
 * Author URI: http://tamilg.in/
 * Version: 0.1
 * Limitation: Tested only with default comment form.
 */

define('CFTF_REPLY_TEXT', 'Leave a Reply');
define('CFTF_REPLY_TO_TEXT', 'Leave a Reply to %s');

// Enable jquery
add_action('init', 'my_script');
function my_script() {
    wp_enqueue_script('jquery');
}

add_filter('comment_reply_link', 'cftf_reply_link');
function cftf_reply_link($link) {
    global $comment;

    $author = get_comment_author();
    $replytext = sprintf( CFTF_REPLY_TO_TEXT, $author );

    $link = str_replace("onclick='return", "onclick='cftf_update_title(\"${replytext}\"); return", $link);
    return $link;
}

add_action('wp_footer', 'cftf_javascript');
function cftf_javascript() {
?>
<script type="text/javascript">
function cftf_update_title(title) {
    var temp = jQuery("#reply-title :first").html();
    jQuery("#reply-title").html(title + '<small>' +  temp + '</small>');
}

jQuery("#cancel-comment-reply-link").live('click', function() {
    var title = "<?php echo CFTF_REPLY_TEXT;?>";
    var temp = jQuery("#reply-title :first").html();
    jQuery("#reply-title").html(title + '<small>' +  temp + '</small>');
});
</script>
<?php
}

Remember it may not work with all themes.

Update: To make it work with the custom theme @Arg Geo uses. Replace the function cftf_javascript() as below

function cftf_javascript() {
?>
<script type="text/javascript">
function cftf_update_title(title) {
    jQuery("#reply-title").html(title);
}

jQuery("#cancel-comment-reply-link").live('click', function() {
    var title = "<?php echo CFTF_REPLY_TEXT;?>";
    jQuery("#reply-title").html(title);
});
</script>
<?php
}
  • Thank you for your answer. Unfortunately didn't work... – Arg Geo Jun 6 '12 at 10:35
  • I suppose you didn't activate the plugin! – tamilsweet Jun 6 '12 at 14:41
  • I activated it tamilsweet! – Arg Geo Jun 6 '12 at 15:35
  • Did you test it with "TwentyEleven" theme without customization ?? – tamilsweet Jun 6 '12 at 16:05
  • No, I tested it in my theme. – Arg Geo Jun 6 '12 at 16:44
1

As of jQuery 1.7, the .live() method is deprecated. Use .on() to attach event handlers.

In the code tamilsweet provided replace:

jQuery("#cancel-comment-reply-link").live('click', function() {

with:

jQuery("#respond").on('click', '#cancel-comment-reply-link', function() {

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