Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
193 views
in Technique[技术] by (71.8m points)

javascript - Adding script, via plugins, in the admin head

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

}


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

1 Answer

0 votes
by (71.8m points)

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();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...