Skip to content

Instantly share code, notes, and snippets.

@mahdiyazdani
Created February 1, 2018 13:12
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mahdiyazdani/446060c640a58b3065d65290d4693c42 to your computer and use it in GitHub Desktop.
Save mahdiyazdani/446060c640a58b3065d65290d4693c42 to your computer and use it in GitHub Desktop.
Since there is NO "get_dynamic_sidebar" in the core, to return sidebar content instead of echoing out use the method below.
<?php
/**
* Gets the sidebar content based on given sidebar ID.
*
* @link https://forums.envato.com/t/get-dynamic-sidebar/76169/3?u=mypreview
* @param string $id ID of required registered sidebar.
* @return string|html The content of sidebar returned with HTML markup without "echo".
*/
if ( ! function_exists( 'prefix_get_dynamic_sidebar' ) ) :
function prefix_get_dynamic_sidebar( $id = NULL ) {
// Bail out, if the ID is empty or missing.
if ( empty( $id ) ) {
return NULL;
}
// Bail out, if there is no sidebar registered with given ID.
if ( ! is_active_sidebar( $id ) ) {
return NULL;
}
$sidebar_id = esc_attr( $id );
ob_start();
dynamic_sidebar( $sidebar_id );
$sidebar_contents = ob_get_clean();
return $sidebar_contents;
}
endif;
@mrwpress
Copy link

Nicely done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment