How to load Javascript in Wordpress Plugin?

0 votes

Can someone show me how to include this javascript file into my wordpress plugin?. I have tried all the wp_enqeue_script() methods but nothing happens.

ok here is my example plugin code with comments explaining what I would like.

<?php
/*
Plugin Name: niroj Test
Plugin URI: http://#.com
Description: A plugin that is used for my javascript tests
Author: niroj Dey
Author URI: http://niroj.com
version: 1.001
*/

include(popup.js); 
/*when I include it this way, it works fine, but gives an error when I activate the plugin
'plugin generated 453 characters ...'
*/

function popup() {
$src = plugins_url('popup.js', __FILE__);
wp_register_script( 'popup', $src );
wp_enqueue_script( 'popup' );
}
/*
when I included it this way, plugin is activated but nothing happens.
*/
?>

So does anybody know how to call this script to work correctly in wordpress plugin?

May 8 in PHP by kartik
• 27,240 points
64 views

1 answer to this question.

0 votes

Hello kartik,

You need to specify when the load should happen, try this.

<?php
/*
Plugin Name: Ava Test
Plugin URI: http://#.com
Description: A plugin that is used for my javascript tests
Author: Niroj dey
Author URI: http://deyniraj.com
version: 1.001
*/

add_action('wp_enqueue_scripts','ava_test_init');

function ava_test_init() {
    wp_enqueue_script( 'ava-test-js', plugins_url( '/js/ava_test_.js', __FILE__ ));
}

Hope this works!!
Thank You!!

answered May 8 by Niroj
• 58,620 points

Related Questions In PHP

0 votes
1 answer

How to connect to SQL Server database from JavaScript in the browser?

Hello @kartik, You shouldn´t use client javascript to ...READ MORE

answered Jul 21 in PHP by Niroj
• 58,620 points
102 views
0 votes
1 answer

How to merge two arrays while keeping keys instead of reindexing in php?

Hello, Considering that you have $replaced = array('1' => ...READ MORE

answered Apr 1 in PHP by Niroj
• 58,620 points
88 views
0 votes
1 answer

How to get the client IP address in PHP ?

Hello, Here is a code sample of a good ...READ MORE

answered Apr 8 in PHP by Niroj
• 58,620 points
1,297 views
0 votes
1 answer

How to log in to phpMyAdmin with WAMP?

Hello @kartik,  Try-Username: root Password: (No password set). Sometimes it doesn't ...READ MORE

answered Apr 9 in PHP by Niroj
• 58,620 points
337 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18 in Laravel by Niroj
• 58,620 points
2,184 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18 in Laravel by Niroj
• 58,620 points
241 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23 in Laravel by Niroj
• 58,620 points
189 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23 in Laravel by Niroj
• 58,620 points
2,716 views
0 votes
1 answer

How to get the current plugin directory in WordPress?

Hello @kartik, This will actually get the result ...READ MORE

answered May 8 in PHP by Niroj
• 58,620 points
165 views
0 votes
1 answer

How to add custom javascript to WordPress Admin?

Hello, Use the admin_enqueue_scripts action and the wp_enqueue_script method to add custom ...READ MORE

answered May 8 in PHP by Niroj
• 58,620 points
60 views