I am trying to use hooks for a js script. The script page does not loads .
But it loads when I login to wp-admin
in themes functions.php
add_action('init', 'test_ajax_load_scripts');
function test_ajax_load_scripts() {
// load our jquery file that sends the $.post request
wp_register_script( "ajax-test", get_template_directory_uri().'/ajax-test.js', array( 'jquery' ) );
// make the ajaxurl var available to the above script
wp_localize_script( 'ajax-test', 'the_ajax_script', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
wp_enqueue_script( 'jquery' );
wp_enqueue_script( 'ajax-test', get_template_directory_uri() .'/ajax-test.js', array( 'jquery' ));
}
ajax-test.js
jQuery(document).ready( function($) {
alert("in ajax-testjs");
});
WordPress uses different hooks for dashboard and frontend.
1.If you want to use scripts or style on front end then see following example-
2.If you want to user script or style on admin side then you need to see following example
Note – you should use unique name for style-name or script-name.
To know more use following link-
https://codex.wordpress.org/Plugin_API/Action_Reference/admin_enqueue_scripts
https://codex.wordpress.org/Function_Reference/wp_enqueue_script