最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to use own css of functionality plugin?

programmeradmin0浏览0评论

I had built a functionality plugin and hooked with actions to another plugin. I had also made a style.css in myplugin/stylesheets/ directory. I placed the code there. I would like to make the styling dependent on my plugin and not my theme.

So far I have been trying this:

function epf_override_style() {
    wp_register_style( 'epf-style', '/stylesheets/style.css' ); //Subdir in plugin directory.
    wp_enqueue_style( 'epf-style' );
}

add_action( 'es_extra_bottom_info','epf_override_style' );

How can I use the stlye.css of my plugin instead of the theme's (child theme's) style.css?

I had built a functionality plugin and hooked with actions to another plugin. I had also made a style.css in myplugin/stylesheets/ directory. I placed the code there. I would like to make the styling dependent on my plugin and not my theme.

So far I have been trying this:

function epf_override_style() {
    wp_register_style( 'epf-style', '/stylesheets/style.css' ); //Subdir in plugin directory.
    wp_enqueue_style( 'epf-style' );
}

add_action( 'es_extra_bottom_info','epf_override_style' );

How can I use the stlye.css of my plugin instead of the theme's (child theme's) style.css?

Share Improve this question edited Jan 26, 2021 at 10:37 Jani asked Jan 26, 2021 at 9:20 JaniJani 156 bronze badges 6
  • You will need to show some code examples, what you have done to make this work, what errors you have - without seeing how you are doing it, it is not possible to offer workable solutions. – Q Studio Commented Jan 26, 2021 at 9:52
  • Thanks for your comment. To be honest, I have done nothing so far, because I don't know how to start connecting. – Jani Commented Jan 26, 2021 at 10:17
  • But, you did "build a plugin" so, why not start by showing that - without additional information your question will be closed - because currently there is no way to answer it. – Q Studio Commented Jan 26, 2021 at 10:31
  • I updated my post. Thank you! – Jani Commented Jan 26, 2021 at 10:38
  • You need to use a function to get teh full path to the file, relative paths are not secure or reliable - look at questions like this - wordpress.stackexchange/questions/127860/… – Q Studio Commented Jan 26, 2021 at 10:46
 |  Show 1 more comment

2 Answers 2

Reset to default 0

You can hook stylesheet_uri which lets you override the URL to the active theme or child theme's style.css:

function epf_stylesheet_uri() {
    return plugins_url( '/stylesheets/style.css', __FILE__ );
}
add_filter( 'stylesheet_uri', 'epf_stylesheet_uri', 10, 1 );

This will then use your plugin's style.css instead of the theme's. But IMO if you want to override the style you should really be changing the theme instead.

What I find working is this:

function epf_override_style() {

    wp_register_style( 'epf-style', '/wp-content/plugins/extended-plugin-functionality/stylesheets/style.css');
    wp_enqueue_style( 'epf-style' );
    
}

add_action( 'wp_print_styles','epf_override_style' );

I have found the solution here: https://www.dummies/web-design-development/wordpress/enhance-wordpress-plugins-css-javascript/

All the comments helped to find the solution.

发布评论

评论列表(0)

  1. 暂无评论