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, 2020 in PHP by kartik
• 37,510 points
509 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, 2020 by Niroj
• 82,880 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, 2020 in PHP by Niroj
• 82,880 points
23,267 views
0 votes
1 answer

How to embed PHP code in JavaScript?

Hello, If your whole JavaScript code gets processed ...READ MORE

answered Nov 9, 2020 in PHP by anonymous
• 82,880 points
746 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, 2020 in PHP by Niroj
• 82,880 points
2,408 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, 2020 in PHP by Niroj
• 82,880 points
6,455 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, 2020 in Laravel by Niroj
• 82,880 points
21,705 views
0 votes
1 answer

What is redirection in Laravel?

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

answered Mar 18, 2020 in Laravel by Niroj
• 82,880 points
2,630 views
0 votes
1 answer

How to install Laravel via composer?

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

answered Mar 23, 2020 in Laravel by Niroj
• 82,880 points
2,486 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, 2020 in Laravel by Niroj
• 82,880 points
41,150 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, 2020 in PHP by Niroj
• 82,880 points
3,377 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, 2020 in PHP by Niroj
• 82,880 points
6,061 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP