0

I am developing a plugin that uses some external PHP library. That PHP library uses js and CSS. I want to enqueue that external library js and CSS using the Wordpress enqueue functions in Wordpress admin area. I have tried using the add_action('admin_enqueue_scripts', array($this, 'wp_db_fb_co_enqueue_scripts')); on the constructor function of the library but it doesn't work. I think I need to add some action hook as well. Please let me know correct way to do this. Thanks

public function __construct() {
    add_action('admin_enqueue_scripts', array($this, 'pdocrud_admin_enqueue_scripts'));        
}
public function pdocrud_admin_enqueue_scripts(){
        wp_enqueue_script('wp-db-fb-co-frontend-script-js', WP_DB_FORM_BUILDER_CRUD_OPERATION_PLUGIN_URL . '/assets/js/frontend-script.js');
    }
0

2 Answers 2

0

Your code seems correct, but the problem might lie somewhere else. You need to make sure that the path to your JS file is correct and the function pdocrud_admin_enqueue_scripts() is being called correctly.

Also, make sure your constructor is being called in the context of a WordPress action that happens after plugins are loaded. Here's a simple example:

class MyPlugin {
    public function __construct() {
        add_action('admin_enqueue_scripts', array($this, 'enqueue_scripts'));
    }

    public function enqueue_scripts() {
        wp_enqueue_script('my-plugin-script', plugins_url('/assets/js/frontend-script.js', __FILE__));
    }
}

// Hook into the 'plugins_loaded' action to create an instance of your plugin.
add_action('plugins_loaded', function() {
    new MyPlugin();
});

In this example, an instance of the MyPlugin class is created on the plugins_loaded action. This is a good place to initialize your plugin, as all the other plugins have been loaded by the time this action fires.

Also, note that in the enqueue_scripts() function, the plugins_url() function is being used to create the URL to the script file. This function generates the correct URL, regardless of where the plugin is installed. The __FILE__ magic constant is being passed as the second argument to plugins_url() to specify that the path should be relative to the current file.

In your case, you'd replace my-plugin-script with wp-db-fb-co-frontend-script-js, and the URL to the script file with your URL.

And remember that wp_enqueue_script() function needs to be called before wp_head() function is executed. The admin_enqueue_scripts action is the appropriate hook to use for enqueuing scripts and styles in the admin area.

-2

The admin_enqueue_scripts action hook is what you want, and then the wp_enqueue_script() and wp_enqueue_style() functions inside to actually register and enqueue the assets to be included.

Untested:

add_action( 'admin_enqueue_scripts', static function () {
    wp_enqueue_script( 'script-handle', 'https://example.com/script.js', array(), '1.0.2' );
    wp_enqueue_style( 'style-handle', 'https://example.com/style.css', array(), '1.0.2' );
} );
4
  • I have used the same add_action('admin_enqueue_scripts', array($this, 'pdocrud_admin_enqueue_scripts')); but it's not calling. I have used same function in the plugin and it works perfectly there. Commented Jun 10, 2023 at 5:49
  • How/when is the class initialized?
    – Caleb
    Commented Jun 10, 2023 at 12:42
  • The code suggested is just the same as what OP have already written. Commented Jun 19, 2023 at 12:51
  • @ray-happyforms The OP edited their question after receiving my answer.
    – Caleb
    Commented Jun 19, 2023 at 13:12

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.