0

I am trying to echo a widgets into my content as so:

function myfunctionid() {
 if(!is_admin()){
   $wppopsidebar = dynamic_sidebar('wp-pop-panel');
   $insert =  '<div>'.$wppopsidebar.'</div>';
   $content = ob_get_clean();
   $content = preg_replace('#<body([^>]*)>#i',"<body$1>{$insert}",$content);
   echo $content;
 }
}

But this does display the contents but not in my div, after a bit of digging I found https://wordpress.org/support/topic/get_dynamic_sidebar-function-needed-or-perhaps-a-hook which explains that what I want to do isn't possible in WP core and I would need to create my own function.

There is a code snippet supplied which is:

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

I have tried to merge my script with theirs but I am getting no where, I don't get any errors in WP but the div disappears from the site. I think it's due to the 2 lots of ob_get_clean, I am still learning aspects of php and I am not familiar with the ob_ stuff yet.

Is anyone able to help me merge the 2 scripts so I can echo my widgets into the div?

  • What happens if you use get_dynamic_sidebar() instead of dynamic_sidebar() in your myfunctionid() function? I don't understand the output buffering in that function either. – birgire Sep 26 '14 at 18:00
  • there is no function get_dynamic_sidebar (see my link above) hence the need to create my own with the second function – Gareth Gillman Sep 26 '14 at 18:32
  • I meant the one you have in your question. – birgire Sep 26 '14 at 18:57
  • I can't believe I missed that, I was trying to add the 2 functions together when I just needed to reference the second function in the first... thanks for the pointer @birgire – Gareth Gillman Sep 26 '14 at 19:40
  • you're welcome, good to hear you solved it. – birgire Sep 26 '14 at 23:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.