Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I've made a Wordpress plugin, and I'm trying to add javascript to the admin head. I've tried but couldn't managed to do it, could anyone help? Bellow is what I was going with:

  function __construct() {        
    add_action('wp_head', 'wpb_hook_javascript');
  }

function wpb_hook_javascript() {
?>
    <script>
      // javscript code 
    </script>
<?php

}


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
197 views
Welcome To Ask or Share your Answers For Others

1 Answer

You need OOP structured solution which is following.

class testing {

public function __construct() {
   add_action( 'admin_head', array($this, 'admin_script'), 10 );
}

public function admin_script() {
  if ( is_admin() ) {
    echo '<script type="text/javascript">

    </script>';
  };
}

}  

// class object/instance 
  new testing();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...