WordPress.org

Support

Support » Plugins and Hacks » Hacks » Use wp_enqueue_style based on user option in widget

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' );
        }
    }
  • The topic ‘Use wp_enqueue_style based on user option in widget’ is closed to new replies.
Skip to toolbar