• At the start of the TwentyTen theme’s sidebar.php file we find the following comment:

    “When we call the dynamic_sidebar() function, it’ll spit out the widgets for that widget area…” This refers to a line of code similar to this:

    <?php if ( ! dynamic_sidebar( 'primary-widget-area' ) ) : ?><?php endif; ?>

    By what mechanism, will this code outputs the widget’s markup when the if clause here, is actually returning a false value?

    Thank you!

Viewing 5 replies - 1 through 5 (of 5 total)
  • What that means:

    if the sidebar named primary-widget-area exists (is registered with WP in functions.php) then any widgets that are active in that widget area are executed. Only if the widget area is not registered, or it is registered but empty (no widgets activated in it) will any code between the IF statement and its endif be executed. That means a theme author can put some default code to be executed in their theme which will run as long as the user hasn’t activated a widget. If they have the widget code is run and the default code in the IF statement is not run.

    Did I explain it clearly enough?

    Thread Starter Lulio Sr.

    (@lulio-sr)

    Thanks stvwif,

    That’s the WP explanation. But, how does WP make it work? According to the PHP Manual

    https://www.php.net/manual/en/control-structures.alternative-syntax.php,

    when you have PHP code like:

    <?php?if?($a?==?5):??>
    A?is?equal?to?5
    <?php?endif;??>

    the HTML block “A?is?equal?to?5” prints out only when the if clause returns true. Otherwise, the code has no output.

    The question is then, how does WP make the code “spit out” the (active) widget’s HTML, when the if clause is actually returning a false?

    Its a good question.

    Within the if clause is
    dynamic_sidebar( 'primary-widget-area' )

    That is a call to the function that displays the widget code. If the function successfully displays some widget code it returns the value of true. If it fails to display widget code it returns false.

    Because the if statement says to execute the code within its block only when the logical statement returned by the expression being evaluated (which in this case is the dynamic sidebar function) is false, it only executes when there was no widget code displayed because that is when the function returns false. If there was widget code displayed the function being evaluated returns true and the if statement doesn’t execute.

    The logical shortcut contained in the IF is that ! is the same as “= false”. When the function returns true, “= false” is not true so the code doesn’t execute.

    I realize that’s a bit confusing, logic-wise. Did you follow it?

    Thread Starter Lulio Sr.

    (@lulio-sr)

    stvwif, Your explanation is excellent. Thank you.

    I find all this very interesting! I guess, it goes with the order that things happen. PHP will first evaluate (run) whatever is within the if wrapper. In this case, the call to the dynamic_sidebar( 'primary-widget-area' ) function outputs the widget markup (that is, provided there are active widgets present) before returning the true/false value.

    That last step determines whether the enclosed HTML block (the default sidebar content) gets printed, or not, depending on the resulting if-clause’s boolean value.

    Did I get it right?

    stvwif, I’m learning my way at developing websites with the WordPress platform. More as a web designer than anything else. Normally, I will generally accept at face value what’s in the documentation (WP codex). However, since a reasonable understanding of dynamic sidebars seem to be essential to working with WP, I decided not to remain with my doubts about the subject. Thanks, for your prompt replies!

    Hi

    What you said is correct. That is not unique to WordPress or PHP. All programming languages have an order in which logic is evaluated. That is the main reason for parentheses in programming code. It evaluates from the inside out – from what is within parentheses. Each part of an If statement expression breaks down to a True or False.

    It gets interesting when AND and OR are involved.

    In other words

    if ( (TRUE) and (TRUE) ) evaluates to true

    if ( (TRUE) and (FALSE) evaluates to false
    the reason this is false is because the expression says both must be true
    —————

    if ( (TRUE) or (TRUE) ) evaluates to true

    if ( (TRUE) or (FALSE) evaluates to true
    the reason this is true is because the expression says
    ” if either A or B is true then do what follows”

    —–

    As you saw, the logical expression itself can run code, so even if the expression evaluates to false, evaluating the logic can cause the program to do things that change its environment or output.

    To me the code you asked about is tricky code and its not at all clear at first glance what that code is doing. The way its coded makes the code shorter than it would otherwise be but doesn’t create clarity. None the less its in the widget code of every WordPress theme. So once you understand it you will know what it means and how it works.

    Last night I had a theme that had default sidebar code I didn’t want running. I added an empty text widget to the sidebar widget area and just that was enough to cause the default sidebar code to stop appearing.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘The meaning of "spitting out" widgets’ is closed to new replies.