开发者

Use wp_enqueue_style based on user option in widget

I currently developing wordpress widget for my website. This widget will pull user latest post in my website and show in their blog.

In the widget there is option for user to use their css or my css for开发者_运维问答 the widget

I use this code in my widget and its work perfectly but this code will always load the css.

add_action( 'widgets_init', 'load_my_widgets' );

function load_my_widgets() {
    register_widget( 'My_Widget' );
    wp_register_style( 'my_widget_css', 'http://mydomain.com/css/my-widget.css' );
    wp_enqueue_style( 'my_widget_css' );
}

The problem is how can I enable the CSS based on the user option? I try something like this but its not working

function widget( $args, $instance ) {
    $own_css = isset( $instance['own_css'] ) ? true : false;

    if ( ! $own_css ) {
        wp_enqueue_style( 'my_widget_css' );
    }
}


Basically, I already answered my own question in wordpress stackexchange

Use wp_enqueue_style based on user option in widget

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜