开发者

The best way to CSS the WP widget to keep style under any theme

开发者 https://www.devze.com 2023-03-06 01:31 出处:网络
I\'m going to make WP widget plugin and want to make it theme independent. What I mean about th开发者_如何学运维is is the fact that I need this widget to keep style set in its CSS regardless of theme

I'm going to make WP widget plugin and want to make it theme independent.

What I mean about th开发者_如何学运维is is the fact that I need this widget to keep style set in its CSS regardless of theme used. I'm worrying that theme will overwrite its style.

What's the best CSS technique to do this ? Should I use style based on widget ID(#) or its Class(.) ?


It's unlikely that someone overwrite your style if you define a container with an ID and have all your style declarations use this ID in the selector.

#MyWidget p {
    color: #ffcc00;
}

#MyWidget p a {
    text-decoration: none;
}

The more specific your selectors are, the more priority they have.


when the wp_head() function is fired, use that to include a stylesheet to your css file..

function wp_mywidget_css() {

    $siteurl = get_option('siteurl');
    $url = $siteurl . '/wp-content/plugins/' . basename(dirname(__FILE__)) . '/styles.css';

    echo "\n\n<!-- your plugin css styles -->\n";
    echo "<link rel='stylesheet' type='text/css' href='$url'>\n";
    echo "<!-- /your plugin css styles -->\n\n";
} 
add_action('wp_head', 'wp_mywidget_css');

place a file called 'styles.css' your plugin folder, which would include the code from the post above, this should stop any theme messing with your styles, just name your widget styles something unique...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号