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

How to add Shortcode (font awesome) in widget title?

I checked your code in my install. It works, except that you made a typo (missing backslash): [icon]cog[/icon] Few notes: You must make sure to enqueue the Font Awsesome stylesheet. You must close the shortcode, like: [icon]cog[/icon] Remember to escape the class name with esc_attr(). Another shortcode idea: [fa icon=”cog”]

Bolding specific word(s)/parts of widget title

This bolds the first words (or entire word, if only one) of my widget titles: add_filter( ‘widget_title’, function ( $title ) { return preg_replace(“/^(.+?)( |$)/”, “<strong>$0</strong>”, $title, 1); }, 10, 1); Breaking down the regex pattern, we have: ^ is the start of the line (.+?) is anything between our next group (in this context, … Read more

Find if widget block is active

One could e.g. get the widget blocks data with: $widget_blocks = get_option( ‘widget_block’ ); and then loop through the array and run has_block() on the contant part, e.g. like (untested): function is_active_block_widget_wpse( $blockname ) $widget_blocks = get_option( ‘widget_block’ ); foreach( (array) $widget_blocks as $widget_block ) { if ( ! empty( $widget_block[‘content’] ) && has_block( $blockname, … Read more