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!
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!
Share Improve this question asked Jun 5, 2012 at 20:03 Arg GeoArg Geo 4591 gold badge8 silver badges20 bronze badges2 Answers
Reset to default 2The 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/ticket/10084 http://core.trac.wordpress/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
}
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() {