Skip to content

Instantly share code, notes, and snippets.

@birgire
Last active February 11, 2019 16:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save birgire/e4a523efadebca2adc83 to your computer and use it in GitHub Desktop.
Save birgire/e4a523efadebca2adc83 to your computer and use it in GitHub Desktop.
WordPress: Modified addComment Javascript function.
var addComment = {
moveForm : function(commId, parentId, respondId, postId) {
var t = this, div, comm = t.I(commId), respond = t.I(respondId), cancel = t.I('cancel-comment-reply-link'), parent = t.I('comment_parent'), post = t.I('comment_post_ID');
if ( ! comm || ! respond || ! cancel || ! parent )
return;
t.respondId = respondId;
postId = postId || false;
if ( ! t.I('wp-temp-form-div') ) {
div = document.createElement('div');
div.id = 'wp-temp-form-div';
div.style.display = 'none';
respond.parentNode.insertBefore(div, respond);
}
respond.style.display = '';
comm.parentNode.insertBefore(respond, comm.nextSibling);
if ( post && postId )
post.value = postId;
parent.value = parentId;
cancel.style.display = '';
cancel.onclick = function() {
var t = addComment, temp = t.I('wp-temp-form-div'), respond = t.I(t.respondId);
if ( ! temp || ! respond )
return;
t.I('comment_parent').value = '0';
temp.parentNode.insertBefore(respond, temp);
temp.parentNode.removeChild(temp);
respond.style.display = 'none';
this.style.display = 'none';
this.onclick = null;
return false;
};
var newfocus = 'comment';
if( 'respond_extra' == respondId ){
newfocus = 'comment_extra';
} else if ( 'respond' == respondId ) {
newfocus = 'comment';
}
try { t.I( newfocus ).focus(); }
catch(e) {}
return false;
},
I : function(e) {
return document.getElementById(e);
}
};
/**
* Replace the default 'comment-reply.min.js' with the custom 'comment-reply-mod.js'
*
* @version 0.0.1
* @file functions.php
*/
function birgire_custom_comment_reply()
{
wp_deregister_script( 'comment-reply' );
wp_enqueue_script( 'comment-reply-mod', get_stylesheet_directory_uri() . '/js/comment-reply-mod.js', array(), '1.0.0', true );
}
add_action( 'wp_enqueue_scripts', 'birgire_custom_comment_reply' );
/**
* Change the default comment form settings
*
* @param array $defaults
* @return array $defaults
*/
function birgire_change_comment_form_defaults( $defaults )
{
$change_some_default_settings = array(
'comment_field' => '<p class="comment-form-comment"><label for="comment_extra">' . _x( 'Extra Comment', 'noun' ) . '</label> <textarea id="comment_extra" name="comment" cols="45" rows="8" aria-required="true"></textarea></p>',
'logged_in_as' => '',
'comment_notes_before' => '',
'comment_notes_after' => '',
'id_form' => 'commentform_extra',
'id_submit' => 'submit_extra',
'title_reply' => __( 'Leave an Extra Reply' ),
'title_reply_to' => __( 'Leave an Extra Reply to %s' ),
'cancel_reply_link' => __( 'Cancel Extra reply' ),
'label_submit' => __( 'Post an Extra Comment' ),
'format' => 'xhtml',
);
return wp_parse_args( $change_some_default_settings, $defaults );
}
// Customize form defaults:
add_action( 'comment_form_defaults', 'birgire_change_comment_form_defaults' );
// Add comments template:
comments_template( '/comments-extra.php', true );
// Remove filter:
remove_action( 'comment_form_defaults', 'birgire_change_comment_form_defaults' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment