Skip to content
Advertisement

weird usage of php open and close tag in callback functions

This is a piece of TwentyTwelve WordPress theme which actually proposed as a good material for learning theme development by WordPress itself.

This function is sent as a callback. But the thing is the weird usage of PHP open and close tags in the code:

I’ve marked them with this comment: /* <—— LIKE THIS */

function twentytwelve_header_style() {
 $text_color = get_header_textcolor();


 if ( $text_color == get_theme_support( 'custom-header', 'default-text-color' ) ) {
    return;
 }

 ?> /* <------ LIKE THIS */
 <style type="text/css" id="twentytwelve-header-css">
 <?php

if ( ! display_header_text() ) :
    ?>
.site-title,
.site-description {
    position: absolute;
    clip: rect(1px 1px 1px 1px); /* IE7 */
    clip: rect(1px, 1px, 1px, 1px);
}
    <?php

    else :
        ?>
    .site-header h1 a,
    .site-header h2 {
        color: #<?php echo $text_color; ?>;
    }
<?php endif; ?>
</style>
<?php   /* <------ LIKE THIS */
}

why are they used in this way?

Advertisement

Answer

It’s not a clean code but it’s not strange as you think, the php tag are closed when the function print directly html, you can change your code like this:

<?php // I think that your code miss opening tag or maybe opening tag is before your snippet

function twentytwelve_header_style() {
 $text_color = get_header_textcolor();


 if ( $text_color == get_theme_support( 'custom-header', 'default-text-color' ) ) {
    return;
 }

 echo '<style type="text/css" id="twentytwelve-header-css">'; /* <------ ECHO AND NO CLOSING PHP TAG */

if ( ! display_header_text() ) :

    echo '.site-title, .site-description { position: absolute; clip: rect(1px 1px 1px 1px); /* IE7 */ clip: rect(1px, 1px, 1px, 1px); }'; /* <------ ECHO AND NO CLOSING PHP TAG */
    else :
    echo '.site-header h1 a, .site-header h2 { color: #<?php echo $text_color; }'; /* <------ ECHO AND NO CLOSING PHP TAG */
endif;
echo '</style>'; /* <------ ECHO AND NO CLOSING PHP TAG */

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement