开发者

how to get is_active_widget on active/runnning page? - wordpress function

开发者 https://www.devze.com 2023-04-12 16:23 出处:网络
I\'m looking for a solution to check is_active_widget on particular page. I got this https://wordpress.stackexchange.com/questions/2302/loading-scripts-only-if-a-particular-shortcode-or-widget-is-pres

I'm looking for a solution to check is_active_widget on particular page.

I got this https://wordpress.stackexchange.com/questions/2302/loading-scripts-only-if-a-particular-shortcode-or-widget-is-present

However the second way of sorich87 still loads the the scripts of a widget on every page where there's no certain active widget on the running/active page. Even when the sidebar of each page has different id & name.

For example:

I have page 'A' (with sidebar 'A1') which consists 'X' active widget and x_scripts in it. Then when I check on page 'B' (with sidebar 'B1') which doesnt consist 'X' widget, it still loads the x_scripts of 'X' widget. Seems that this is_active_widget function only check for general activated widget, not o开发者_如何学编程n the running page.

This is my code:

class X extends WP_Widget
{
        function X(){
            $widget_ops = array('description' => 'X widget');
            $control_ops = array('width' => 200, 'height' => 500);
            parent::WP_Widget(false,$name='X-widget',$widget_ops,$control_ops);

            if ( is_active_widget(false, false, $this->id_base, true) ){
                add_action('wp_head', 'x_styles',0);
                add_action( 'wp_footer', 'x_scripts' );
            }
        }

    //rest of functions here
}

How to prevent this?


Yes it does, is_active_widget() get the value from options saved on database. Therefore it will return true for any widgets that is active. The is_active_sidebar() behaves the same, even though the sidebar is not loaded in specific pages, the returned value will always be true as long as the sidebar is registered.

Both functions check the global $wp_registered_widgets, $_wp_sidebars_widgets, $sidebars_widgets which values are generated from register_sidebar() and widgets that has been added to the sidebar.

Unfortunately, there's no workaround for this. Since dynamic_sidebar() is loaded after wp_head(), it's impossible to hook a script or stylesheet conditionally to it. The script or styles or anything you hook to wp_head will always there even the sidebar is loaded or not, unless widget is inactive.

Maybe, logically, a different possibility is there for adding a script on wp_footer() for a widget, in this case, you can set a global variable or define a constant in your widget function. Therefore, the scripts will be loaded only if widget is active.

Well, there's my two cents


Thanks to AriePutranto, who pushed me in the right direction. The same way you tell if there's an active widget in the widget($instance) function is how you decide whether to include your js/css files.

If you're fine with your includes being in the footer, then this works!

// Create the widget output.
    public function widget($args, $instance)
    {
        if (! empty($instance)) {
            add_action('wp_footer', array(&$this, 'enqueueWidgetIncludes'));
            $this->printWidget();           
        }
    }

    private function enqueueWidgetIncludes() {
        //css:

        wp_enqueue_style(/*[code removed]*/);

        wp_enqueue_script(/*[code removed]*/);
    }


I think the easiest solution to this problem would be to simply edit the sidebar widgets themselves. Just use wp_enqueue_script() & wp_enqueue_style() in the plugin files and it'll load them only on the pages where the separate widgets are displayed.

0

精彩评论

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