WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 8 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


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.