WordPress Widget multiple use

You need to implement your Widget using the Widgets API, so that WordPress knows how to make multiple instances of the Widget. Your Widget declaration should take the following format: class My_Widget extends WP_Widget { function My_Widget() { // widget actual processes } function form($instance) { // outputs the options form on admin } function … Read more

wordpress widget textbox in the sidebar

check your sidebar.php and functions.php of the theme, the sidebar define the markup outside the widget; the function register_sidebar() defines the markup before content of the widgets; this markup you must change. See the codex for this function: codex register_sidebar() Edit Specifically, you need to look at the before_widget and after_widget parameters of the register_sidebar() … Read more

Use wp_enqueue_style based on user option in widget

This is sort of sloppy as the style will still be enqueued regardless. In the code to display your widget, change the CSS selectors based on whether or not the user selected own style: <?php $prefix = $instance[‘own_style’] ? ‘ownstyle_’ : ‘pluginstyle_’; //then…. ?> <div id=”<?php echo $prefix; ?>selector”> …</div> etc A user would be … Read more

Widget queries even when there are no sidebars?

Does unregistering them the also disable the database queries? Goes in functions.php: //Unregister all Default Widgets function unregister_default_wp_widgets() { unregister_widget(‘WP_Widget_Pages’); unregister_widget(‘WP_Widget_Calendar’); unregister_widget(‘WP_Widget_Archives’); unregister_widget(‘WP_Widget_Links’); unregister_widget(‘WP_Widget_Meta’); unregister_widget(‘WP_Widget_Search’); unregister_widget(‘WP_Widget_Text’); unregister_widget(‘WP_Widget_Categories’); unregister_widget(‘WP_Widget_Recent_Posts’); unregister_widget(‘WP_Widget_Recent_Comments’); unregister_widget(‘WP_Widget_RSS’); unregister_widget(‘WP_Widget_Tag_Cloud’); } add_action(‘widgets_init’, ‘unregister_default_wp_widgets’, 1);

Elegantly using JavaScript on widget admin forms

One way around this I’ve discovered since posting the above question is to instantiate inline via WP_Widget::form(), with wp_enqueue_script() loading the necessary JavaScript (exactly as mentioned above) — except, and here’s the key part, load the JavaScript in the header instead of the footer (fifth argument of wp_enqueue_script()). Which makes a lot of sense now … Read more

Why use dynamic_sidebar() conditionally?

To quote the Codex The return value should be used to determine whether to display a static sidebar. This ensures that your theme will look good even when the Widgets plug-in is not active. So essentially you can use is to display other content if the user has not activated any widgets in the sidebar.

List sidebars on a page

There actually are filters coming with WP 3.9+: do_action( ‘dynamic_sidebar_before’, $index, false ); do_action( ‘dynamic_sidebar_after’, $index, false ); apply_filters( ‘dynamic_sidebar_has_widgets’, false, $index ); The 2nd argument indicates if the sidebar has widgets. Current workaround: Hook into sanitize_title() as this will hold the current sidebar name/ID. Then hook into wp_get_sidebars_widgets() as this is where you already … Read more