I wrote a plugin but it keeps adding custom javascript into section not into section of every page.
Can anyone help me or give me a hint how to add js into section?
public constructor {
add_action( 'init', array( $this, 'custom_js_register' ) );
add_action( 'wp_head', array( $this, 'custom_js_print' ) );
}
function custom_js_register() {
wp_register_script('custom_button', 'http://xxx.xxx.xxx/js/Button.js');
}
function custom_js_print() {
wp_enqueue_script('custom_button');
}
also instead of just loading js, I want to be able to do something like this.
<script type="text/javascript" src="http://xxx.xxx.xxx/js/Button.js" charset="UTF-8">
</script>
You must use the hook to enqueue the scripts in WordPress.
Use the hook
wp_enqueue_scripts
for the front end andadmin_enqueue_scripts
for the back end side.If you are enqueueing scripts and styles, you will want to use one of these three hooks:
wp_enqueue_scripts
(for the frontend)login_enqueue_scripts
(for thelogin screen)
admin_enqueue_scripts
(for the admin dashboard)It is enough to use one method for register and enqueue of scripts. The difference is the benefit for other developers to de-register the script.
Als the hint to not use the static address, with
http
. It is better to use the wp functionplugins_url
. Also a example: