I'm looking to add a second dynamic_sidebar to a theme's sidebar.php that will only be displayed if the height of my main content area is greater than a certain amount. If I had some magic hybrid of jQuery and php, and I wanted this to happen if the main content area (#content) was taller than 700px I'd do something like:

 if ( jQuery('#header-wrap').height() > 700 ) {
     dynamic_sidebar( 'sidebar-extra' );

Is this possible using real phg? I know I could use jQuery hide() inside that conditional, but I'd rather not load the sidebar and mess with my layout if I didn't have to.

1 Answer 1


The "magic hybrid of jQuery and php" is called "AJAX", at least in this case. You will need to use the Javascript to conditionally make another request to the server. The PHP runs on the server and so has no idea how tall the page is. The Javascript runs in the browser and can work out the window height but can't run the PHP directly. The only choice is AJAX.

WordPress has an AJAX API, that makes AJAX requests very easy. There are plenty of examples in the Codex for using the AJAX API and plenty of questions here about it as well. Get started. If you have trouble, edit your question with the specifics.

Your Answer

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

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