0

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?

2
  • I think you just have to change your hook ! admin_enqueue_scripts for admin instead of wp_enqueue_scripts
    – Elex
    Dec 10, 2017 at 12:45
  • Great job, thanks a lot! :) Paste it as a question, and I will accept it
    – Ognj3n
    Dec 10, 2017 at 12:51

1 Answer 1

2

I think you just have to change your hook ! admin_enqueue_scripts for admin instead of wp_enqueue_scripts :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.