0

I am trying to add custom CSS for plugin admin Page. If I use below code then it is working.

wp_enqueue_style( 'cricket-info-admin-styles', plugins_url('') . '/wp-cricket-info/assets/css/admin_styles.css', false, '1.0.0', 'all' );

But if I use below code it is not working.

wp_enqueue_style( 'cricket-info-admin-styles', plugins_url('wp-cricket-info/assets/css/admin_styles.css', __FILE__), false, '1.0.0', 'all' );

Why second code is not working ?

2 Answers 2

2

I guess you're using this code in a wrong file other than the main plugin file (usually /wp-content/plugins/wp-cricket-info/wp-cricket-info.php). So, the file you're referencing when enqueueing is actually incorrect.

Follow the steps below-

  1. Define a constant in the main plugin file that references the file itself.

define( 'WP_CRICKET_INFO', __FILE__ );

  1. When enqueueing the CSS, use that constant as the second parameter.

wp_enqueue_style( 'cricket-info-admin-styles', plugins_url( 'assets/css/admin_styles.css', WP_CRICKET_INFO ), false, '1.0.0', 'all' );

3

It's likely because plugins_url() returned an invalid URL like so: ( note the wp-cricket-info/wp-cricket-info/ )

https://example.com/wp-content/plugins/wp-cricket-info/wp-cricket-info/assets/css/admin_styles.css

So if you specify the 2nd parameter, which is a full path to a file inside a plugin or mu-plugin, then the 1st parameter should be a path relative to the directory of that plugin.

  • Correct: plugins_url('assets/css/admin_styles.css', __FILE__)
  • Incorrect: plugins_url('wp-cricket-info/assets/css/admin_styles.css', __FILE__)

And with plugins_url(''), your code worked correctly because the directory (wp-cricket-info/) was not "doubled".

3
  • Thanks @Sally CJ. If I use this code wp_enqueue_style( 'cricket-info-admin-styles', plugins_url('assets/css/admin_styles.css', __FILE__), false, '1.0.0', 'all' ); then I am getting output http://127.0.0.1/wordpressbosta/wordpressplugincricket/wp-content/plugins/wp-cricket-info/inc/Admin/assets/css/admin_styles.css?ver=1.0.0. But I need output http://127.0.0.1/wordpressbosta/wordpressplugincricket/wp-content/plugins/wp-cricket-info/assets/css/admin_styles.css?ver=1.0.0. Thanks.
    – abu abu
    Aug 6 at 6:07
  • 1
    In that case, then you would want to define a constant which stores the full absolute path to the main plugin file, e.g. if the file is at wp-content/plugins/wp-cricket-info/wp-cricket-info.php, then add define( 'WP_CRICKET_INFO_PLUGIN_FILE', __FILE__ ); at the top in that file. Then pass that constant to plugins_url(), i.e. plugins_url( 'assets/css/admin_styles.css', WP_CRICKET_INFO_PLUGIN_FILE );.
    – Sally CJ
    Aug 6 at 6:43
  • 1
    Or you could define the full URL to the plugin folder, e.g. define( 'WP_CRICKET_INFO_PLUGIN_URL', plugin_dir_url( __FILE__ ) );, and use that constant from within any PHP files in your plugin folder, e.g. WP_CRICKET_INFO_PLUGIN_URL . 'assets/css/admin_styles.css'.
    – Sally CJ
    Aug 6 at 6:46

Your Answer

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

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