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

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

Creating your own widgets: using cache?

take a look at WordPress Transients API which offers a simple and standardized way of storing cached data in the database temporarily by giving it a custom name and a timeframe after which it will expire and be deleted. The transients API is very similar to the Options API but with the added feature of … Read more

How to limit wp_get_archives to show months for the X years only

If using “regular” query in theme file (sidebar.php for example), this code will help do the trick: <?php wp_get_archives(‘type=monthly&limit=12’); ?> The above (and below) will only show the last 12 months. However if using a widget, then add this code to your functions.php file: function my_limit_archives( $args ) { $args[‘limit’] = 12; return $args; } … Read more

Image picker widget

Try this code. class Image_Picker extends WP_Widget { function Image_Picker() { $widget_ops = array(‘classname’ => ‘Image_Picker’, ‘description’ => ‘Pick 2 images from media library’); $this->WP_Widget(‘Image_Picker’, ‘Image Picker’, $widget_ops); } function form($instance) { //WIDGET BACK-END SETTINGS $instance = wp_parse_args((array) $instance, array(‘link1’ => ”, ‘link2’ => ”)); $link1 = $instance[‘link1’]; $link2 = $instance[‘link2’]; $images = new WP_Query( … Read more

How to insert widget areas specific to certain pages (or posts, etc.)?

Use Conditional Tags to show content only if a certain condition is met. In your case, you’re probably looking to use is_front_page(). <aside> <ul> <?php if ( function_exists( ‘dynamic_sidebar’ ) ) { if ( is_front_page() ) { if ( ! dynamic_sidebar( ‘frontpage-widget-area’ ) ) { echo ‘<li>No sidebars for the frontpage.</li>’; // some default output … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)