Skip to content

Instantly share code, notes, and snippets.

@fikrirasyid
Last active August 29, 2015 14:16
Show Gist options
  • Save fikrirasyid/7242efb1b8359d23f619 to your computer and use it in GitHub Desktop.
Save fikrirasyid/7242efb1b8359d23f619 to your computer and use it in GitHub Desktop.
Ajaxifying Comment - Load Javascript script & its Javascript params
/**
* Load the script
* Most of the time, the script will be used at is_singular(). Make this pluggable tho, just in case the single page is displayed as overlay on other page
*/
function simple_ajax_comment_script(){
if( apply_filters( 'simple_ajax_comment_is_used', is_singular() ) ){
/**
* Load the js script
*/
wp_enqueue_script( 'simple-ajax-comment-script', plugin_dir_url( __FILE__ ) . 'js/simple-ajax-comment.js' , array( 'jquery', 'jquery-form' ), '1.0.0' );
/**
* Error messages can be obtained from /wp-includes/comment.php. Look for a line which uses wp_die()
* Its error code can be used to identify proper error response.
*/
wp_localize_script( 'simple-ajax-comment-script', 'simple_ajax_comment_params', apply_filters( 'simple_ajax_comment_params', array(
'error_messages' => array(
'409' => __( 'Duplicate comment detected; it looks as though you have already said that!' ),
'429' => __( 'You are posting comments too quickly. Slow down.' )
)
)));
}
}
add_action( 'wp_enqueue_scripts', 'simple_ajax_comment_script' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment