WordPress.org

Ready to get started?Download WordPress

Forums

Use wp_enqueue_style based on user option in widget (1 post)

  1. skycrew
    Member
    Posted 3 years ago #

    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' );
        }
    }

Topic Closed

This topic has been closed to new replies.

About this Topic