Skip to content

Instantly share code, notes, and snippets.

@SalmanRavoof
Last active January 1, 2021 16:12
Show Gist options
  • Save SalmanRavoof/0d478b0039c4249148c526afd92c4c92 to your computer and use it in GitHub Desktop.
Save SalmanRavoof/0d478b0039c4249148c526afd92c4c92 to your computer and use it in GitHub Desktop.
Enqueue jQuery library as well as your plugin’s custom JavaScript file by adding it to your plugin.
<?php // used here only for enabling syntax highlighting. Leave this out if it's already included in your plugin file.
// Fires after WordPress has finished loading, but before any headers are sent.
add_action( 'init', 'script_enqueuer' );
function script_enqueuer() {
// Register the JS file with a unique handle, file location, and an array of dependencies
wp_register_script( "liker_script", plugin_dir_url(__FILE__).'liker_script.js', array('jquery') );
// localize the script to your domain name, so that you can reference the url to admin-ajax.php file easily
wp_localize_script( 'liker_script', 'myAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' )));
// enqueue jQuery library and the script you registered above
wp_enqueue_script( 'jquery' );
wp_enqueue_script( 'liker_script' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment