dongshao6591
2017-12-10 09:13
浏览 99
已采纳

通过插件在管理页面上加载自定义js文件

I'm new to the world of Wordpress and I just started developing my first plugin. When it's activated it should load one javascript file to wp-admin/post-new.php (Add new post) page.

Here is how I tried to do this:

Plugin

class WP_Blog_Customizer{
    function __construct() {
        add_action( 'wp_enqueue_scripts', array($this, 'load_dependencies') ); 
        register_activation_hook( __FILE__, array( $this, 'wpa_install' ) );
        register_deactivation_hook( __FILE__, array( $this, 'wpa_uninstall' ) );
    }
    public function load_dependencies(){
        wp_enqueue_script('blog-customizer', plugins_url('js/blog-customizer.js', __FILE__),array('jquery'),'1.0.0', true);
    }
}
new WP_Blog_Customizer();

wp-admin/post-new.php

if(is_plugin_active( 'blog-customizer/blog-customizer.php' )){
        $plugin = new WP_Blog_Customizer();
}

Shouldn't this add_action( 'wp_enqueue_scripts', array($this, 'load_dependencies') ); from the __construct of my plugin class include this js file?

Note

This js file is located under the js folder in my plugin's folder, so the path is correct.

Can anyone tell me why this is not working, and how to make it work?

图片转代码服务由CSDN问答提供 功能建议

我是 Wordpress 的新手,我刚刚开始开发我的第一个< 代码>插件。 当它被激活时,它应该将一个javascript文件加载到 wp-admin / post-new.php (添加新帖子)页面。

这是我试过的方式 要做到这一点:

插件

 类WP_Blog_Customizer {
 function __construct(){
 add_action  ('wp_enqueue_scripts',array($ this,'load_dependencies'));  
 register_activation_hook(__ FILE __,array($ this,'wpa_install')); 
 register_deactivation_hook(__ FILE __,array($ this,'wpa_uninstall')); 
} 
公共函数load_dependencies(){
 wp_enqueue_script('  blog-customizer',plugins_url('js / blog-customizer.js',__ FILE __),array('jquery'),'1.0.0',true); 
} 
} 
new WP_Blog_Customizer(); 
    
 
 

wp-admin / post-new.php

  if(is_plugin_active('  blog-customizer / blog-customizer.php')){
 $ plugin = new WP_Blog_Customizer(); 
} 
   
 
 

不应该 我的插件类的 __ construct 中的add_action('wp_enqueue_scripts',array($ this,'load_dependencies')); 包含这个js文件?

注意

此js文件位于我的插件文件夹中的js文件夹下,因此路径正确。

任何人都可以告诉我为什么这不起作用,以及如何使它工作?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • doutiaosu2310 2017-12-10 12:48
    最佳回答

    For loading scripts in admin side we have to use admin_enqueue_scripts hook. So something like this should do the job:

    class WP_Blog_Customizer{
        function __construct() {
            add_action( 'admin_enqueue_scripts', array($this, 'load_dependencies') ); 
            //...other constructor things
        }
        public function load_dependencies( $hook ){
            if ( $hook == 'post-new.php' ) { // for loading script also on post edit screen use ( $hook == 'post-new.php' || $hook == 'post.php' )
                wp_enqueue_script('blog-customizer', plugins_url('js/blog-customizer.js', __FILE__),array('jquery'),'1.0.0', true);
            }
        }
    }
    new WP_Blog_Customizer();
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题