Take the 2-minute tour ×

I want to get a sidebar and save it in a Variable,
This is impossible with the get_sidebar function since it simply prints out
the sidebar, is there another function that I can use?

share|improve this question
What do you want to do with this variable? –  Chip Bennett Jul 14 '11 at 13:12
I want to add widgets from a "page" content, so I'm trying to replace tags like %SIDEBAR_3% to the actual sidebar, so I'm using str_replace but I need a function that returns something –  Asaf Jul 14 '11 at 13:18

2 Answers 2

up vote 6 down vote accepted

You probably need to use output buffering, if I understand your question correctly.

Try something like this:

$sidebar = ob_get_contents();

That will put the output of the sidebar into the variable $sidebar, which you can then process and return from your filter on the page content.

share|improve this answer
worked for me! thanks! had to save the entire footer html output in a transient. thanks! –  Amit Aug 31 '11 at 21:14

The get_sidebar() function is an include function, used to include sidebar.php.

The function I think you're after is dynamic_sidebar() (Codex ref), assuming you know the ID of the dynamic sidebar you want to use.

share|improve this answer

Your Answer


