Last active
January 1, 2021 16:12
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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