Search code examples
javascriptwordpresswordpress-plugin-creation

How To Include javascript in my WordPress plugin?


  function my_scripts() {

wp_enqueue_script( 'new-1', get_site_url() . '/wp-content/plugins/domain-plugin/new1.js', array( 
'jquery' ),'',true );
      }
 add_action( 'wp_enqueue_scripts', 'my_scripts' );

this is my code what i am doing worng in it. it isnt including my js file to plugin on admin side following is my js file code

function change_dom_field()
{
var x = document.getElementById("email");
var y = document.getElementById("domain");
var z = document.getElementById("dom_button");
var a = document.getElementById("mail_button");
document.getElementById("flag").value="email";
if (x.style.display === "none") {
x.style.display = "block";
y.style.display = "none";
z.style.display = "block";
a.style.display = "none";
}

//alert('abc');

}


Solution

  • If you are trying to add your script in your backend you should use this:

    add_action( 'admin_enqueue_scripts', 'my_scripts' );
    

    instead of

    add_action( 'wp_enqueue_scripts', 'my_scripts' );
    

    Because admin hook is different. Documentation is here: https://developer.wordpress.org/reference/hooks/admin_enqueue_scripts/