Add javascript to Wordpress plugin

Asked

Viewed 274 times

2

I registered my Javascript file in my plugin but the functions of click and etc. do not work.

I registered my main plugin file with the action:

function add_scripts_to_plugin()
{
     wp_enqueue_script( 'birthdate_celebrate_js', dirname(__FILE__) . '/includes/assets/js/main.js', array( 'jquery' ));
}
add_action('wp_enqueue_scripts', 'add_scripts_to_plugin');

The Javascript file:

jQuery( function( $ ) {
    $('#birthdate_celebrate_send_mails').on('click', function () {
        console.log('clicked');
    });
});

The plugin loads normally and shows no error in the console or in PHP, but even so the eventListener click is not added to button.

  • What is the link to the site for me to view?

  • I’m developing locally but the link to the repository is this

  • You tried to replace the line $('#birthdate_celebrate_send_mails') for jQuery('#birthdate_celebrate_send_mails') ?

  • Yes Wendell, within the function jQuery either write down the name or use the $.

  • The button is added to the page after the initial upload?

  • Yes Ricardo, the function wp_enqueue_scripts puts the script in the footer after loading using the wp_footer()

  • Actually Ricardo wanted to know if the button in question is included via some other script, instead of being in the HTML source.

Show 2 more comments

3 answers

4


I found the answer in this stack topic in English.

To include the archive with wp_enqueue_script be a plugin, the function shall be used plugin_dir_url and not get_template_directory_uri(), because this function returns a URL that points to the current theme being used.

Resolution:

function birthdate_celebrate_add_interactions_to_plugin()
{
    wp_enqueue_script( 'birthdate_celebrate', plugin_dir_url( __FILE__ ) . 'includes/assets/js/birthdate_celebrate.js', array( 'jquery' ), '1.0');
}
add_action('admin_enqueue_scripts', 'birthdate_celebrate_add_interactions_to_plugin');

2

I see two things that can potentially give problem there, but nothing guarantees that they are. The first is to use dirname(__FILE__) instead of get_template_directory_uri():

function add_scripts_to_plugin()
{
     wp_enqueue_script( 'birthdate_celebrate_js', get_template_directory_uri() . '/includes/assets/js/main.js', array( 'jquery' ));
}
add_action('wp_enqueue_scripts', 'add_scripts_to_plugin');

The second is to place a click event on an element that is on the page from the page load, and delegate to the button:

jQuery( function( $ ) {
    // troque 'body' por um outro seletor mais próximo, talvez um pai direto do botão que quer alcançar.
    $('body').on('click', '#birthdate_celebrate_send_mails' , function () {
        console.log('clicked');
    });
});
  • I changed to use the function get_template_directory_uri() and applied the same method to js and even then nothing appears, the interesting thing is that if I call in the browser console only $('#...') I get a mistake saying that $ is not a function, but if I use jQuery works...tried to use jQuery in the file and even then it doesn’t work.

  • On the console you won’t be able to use $ because the symbol is set before by the same browser. If with jQuery('#...') works then it remains to know if the file is being properly loaded. It appears right in the tab network?

  • ma aba network there is nothing with the name I defined

  • It does not appear? Strange. Do a test putting at the beginning of the file console.log('meu script incluído'); and see if the message appears in the Browser console. If it does not appear, check the name and path of the file, they may be wrong.

1

Your code is correct, I did a test on a page only with a button with this id and it worked.

See if at the time you try to attach the event the object already exists, put before an instruction to print the result of $('#birthdate_celebrate_send_mails'):

jQuery( function( $ ) {
    console.log($('#birthdate_celebrate_send_mails'));
    $('#birthdate_celebrate_send_mails').on('click', function () {
        console.log('clicked');
    });
});

Another way to do it, if the object is included later, would be like this:

    $(document).on('click', '#birthdate_celebrate_send_mails', function () {
        console.log('clicked');
    });
  • here still doesn’t work, I’m using the Woocommerce theme storefront only to include the info.

  • tried to put the command console.log($('#birthdate_celebrate_send_mails')); to see what he finds, if he finds something?

  • nothing appears

  • Then the button should be created after the page load. Then you should use the second version of me or Ricardo, which are similar.

  • I’ve tried both and no one works :/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.