In this tutorial, I have shared about how to use script and styles in WordPress. You can register your scripts and styles by using the wp_register_script()
function. See the examples below:
1 2 3 4 5 6 7 8 9 | <?php function wpb_adding_scripts() { wp_register_script('my_amazing_script', plugins_url('amazing_script.js', __FILE__), array('jquery'),'1.1', true); wp_enqueue_script('my_amazing_script'); } add_action( 'wp_enqueue_scripts', 'wpb_adding_scripts' ); ?> |
Use bellow function for add styles in WordPress.
1 2 3 4 5 | function your_function_name() { wp_enqueue_style( 'style', plugins_url( 'put your css file path', __FILE__ ) ); } add_action( 'wp_enqueue_scripts', 'your_function_name' ); |
If you want to add custom java script code, use below code (But is bad)
1 2 3 4 5 6 7 8 | function admin_load_js() { ?> <script type="text/javascript"> //add your code here </script> <?php } add_action('admin_head', 'admin_load_js'); |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.