Make WordPress Core

Opened 14 years ago

Last modified 5 years ago

#13169 reopened enhancement

Return Dynamic Sidebars with get_dynamic_sidebar

Reported by: w3prodigy's profile w3prodigy Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Widgets Keywords:
Focuses: Cc:

Description

Currently there is no available function to return the contents of a dynamic sidebar. The following code enables developers to return and assign the contents of a dynamic sidebar to a variable within their code.

function get_dynamic_sidebar($index = 1) 
{
	$sidebar_contents = "";
	ob_start();
	dynamic_sidebar($index);
	$sidebar_contents = ob_get_contents();
	ob_end_clean();
	return $sidebar_contents;
}

Change History (18)

#1 @w3prodigy
14 years ago

Thanks to Filosofo for pointing out that we can use ob_get_clean() instead of ob_get_contents() and ob_end_clean(). The use of output buffering is a simple solution until further can be developed.

function get_dynamic_sidebar($index = 1) 
{
	$sidebar_contents = "";
	ob_start();
	dynamic_sidebar($index);
	$sidebar_contents = ob_get_clean();
	return $sidebar_contents;
}

#2 @iseulde
11 years ago

  • Component changed from General to Widgets

#3 follow-up: @ocean90
11 years ago

  • Milestone Future Release deleted
  • Resolution set to maybelater
  • Status changed from new to closed

This is the first request for a get variant of dynamic_sidebar(). Currently I don't see how core or a theme could benefit of it.

Marked as maybelater.

#4 in reply to: ↑ 3 @cbos
10 years ago

  • Resolution maybelater deleted
  • Status changed from closed to reopened

Replying to ocean90:

This is the first request for a get variant of dynamic_sidebar(). Currently I don't see how core or a theme could benefit of it.

Marked as maybelater.

Could use it right now as part of a function in which the html is being concatenated to a string. It would seem reasonable to be consistent and always allow html to be returned as well as echoed.

#5 @SergeyBiryukov
10 years ago

  • Milestone set to Awaiting Review

#6 follow-up: @MadtownLems
10 years ago

For what it's worth, I was hoping get_dynamic_sidebar existed so that I could easily cache the html output of my sidebars.

#7 @cardiganmedia
9 years ago

Something like this would make it much easier to loop through and count widgets, which can be helpful when crafting layout markup.

#8 in reply to: ↑ 6 @westonruter
9 years ago

Replying to MadtownLems:

For what it's worth, I was hoping get_dynamic_sidebar existed so that I could easily cache the html output of my sidebars.

You can already do this, right? For instance:

<?php
function my_get_dynamic_sidebar( $sidebar_id ) {
        if ( ! is_user_logged_in() ) {
                $content = wp_cache_get( $sidebar_id, 'cached-sidebar' );
                if ( false !== $content ) {
                        return $content;
                }
        }
        ob_start();
        dynamic_sidebar( $sidebar_id );
        $content = ob_get_clean();
        if ( ! is_user_logged_in() ) {
                wp_cache_set( $sidebar_id, $content, 'cached-sidebar', 5 * MINUTE_IN_SECONDS );
        }
        return $content;
}

#9 follow-up: @prometh
9 years ago

This issue is 6 years old. WordPress is pathetic.

#10 in reply to: ↑ 9 @DrewAPicture
9 years ago

Replying to prometh:

This issue is 6 years old. WordPress is pathetic.

You are welcome to participate in the process here. The attitude you've expressed here is not. Please try to keep in mind that WordPress is by and large a community of volunteers working toward improving it. Comments like this do nothing to drive progress forward.

#11 follow-up: @prometh
9 years ago

I contribute to communities that are not dying (Node.js). This should've been fixed 6 years ago and it's wasted my time.

#12 in reply to: ↑ 11 @welcher
9 years ago

Replying to prometh:

I contribute to communities that are not dying (Node.js). This should've been fixed 6 years ago and it's wasted my time.

You are welcome to submit a patch and get this fixed. Otherwise, please respect everyone else's time by not posting negative, non-productive comments.

#13 @tikitariki
9 years ago

I, too, would like to get a return variant of dynamic_sidebar. There's no easy way to get a count of WordPress widgets. I figured out a way using a hook for widgets, but I was stopped in my tracks yet again because the widget hooks won't fire off until the widgets are outputted, which is when dynamic_sidebar is called. I need that count before calling dynamic_sidebar, but since it gets echoed out, I can't arrange the content appropriately without having to use ob_ functions. In fact, in this scenario, to get my desired result, I had to call dynamic_sidebar twice, once in an output buffer to count them, and second time to show the content.

NOTE: This is my first Core Trac comment, if I did anything wrong in contributing to this ticket.

Last edited 9 years ago by tikitariki (previous) (diff)

#14 follow-up: @westonruter
9 years ago

@tikitariki can't you use wp_get_sidebars_widgets() to get the count of widgets in a sidebar?

#15 @markhowellsmead
8 years ago

Additional use case: two sidebars to be wrapped in a single outer HTML container, but only if one or both of them isn't empty.

#16 @jobst
6 years ago

Since @SergeyBiryukov put it as "awaiting review" I have you included for review again.

I, too, would like to have a function called "get_dynamic_sidebar", preferably as a core function so we do not get the overhead of the ob_* based functions.

Alternatively there could be a flag added as parm for "dynamic_sidebar()" which stops the output but rather returns it.

One of the obvious uses would be to scan for a string and replace:

 $foobar=get_dynamic_sidebar('CopyrightNotice');
 $foobar=str_replace("%THEYEAR%",date('Y'),$foobar);
 echo $foobar;

Not sure why some core developers think this should not be implemented as it is pretty obvious to me if you want to dynamically replace content of a sidebar (or combine them as explained above).

I have many websites that have a copyright notice in the footer that have a year in it. Easy to change when the content could be searched.

#17 in reply to: ↑ 14 @rgblab
6 years ago

Replying to westonruter:

@tikitariki can't you use wp_get_sidebars_widgets() to get the count of widgets in a sidebar?

This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only in other core functions. It is listed here for completeness.

https://developer.wordpress.org/reference/functions/wp_get_sidebars_widgets/

#18 @log1x
5 years ago

Would love to see this added to core sooner than later.

Note: See TracTickets for help on using tickets.