Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

WordPress WordPress Customizer API Native WordPress Customizer Settings Custom Header Images, Colors and Display Text Option

Montalvo Miguelo
Montalvo Miguelo
24,789 Points

Why you are closing the php tag in your wp-head-callback ?

<?php
// Callback function for updating header styles
function wpt_style_header() {

  $text_color = get_header_textcolor();

  ?>  // <<<--- THIS

  <style type="text/css">

  #header .site-title a {
    color: #<?php echo esc_attr( $text_color ); ?>;
  }

  <?php if(display_header_text() != true): ?>
  .site-title {
    display: none;
  }
  <?php endif; ?>

  </style>
  <?php // <<<--- THIS

}
?>

4 Answers

Chris Shaw
Chris Shaw
26,662 Points

Hi Montalvo,

This is because we're using static HTML within the function, this is a common practice and is something you will see quite a lot during WordPress theme development.

Hope that helps.

if you close the php tags you can write pure html. You don't have to do this as you can just use php 'echo' to echo out elements. It is just far easier to read if you close the php and then write in pure html, you then open up the php tags again after completing the html

<?php 
function hello() {
    echo '<h1>hello world</h1><p>header with paragraph underneath</p>';
}
?>

is exactly the same as:

<?php function hello() { ?>
    <h1>hello world</h1>
    <p>header with paragraph underneath</p>
<?php } ?>
Montalvo Miguelo
Montalvo Miguelo
24,789 Points

Is a kind of echoing the html or what does it mean?... It breaks if I remove those tags.

<?php

function foo() {
  ?> // closing
  <style>/* css here*/</style>
  <?php // opening 
}
Glenn Basgaard
Glenn Basgaard
7,987 Points

Figured I'd give another explanation attempt at this just cause. The way I understand it is that you can't write html directly inside of php. Sure you can echo it like the above answers state, but in this example the <style> tag isn't being echoed in php.

Therefore that's why the php is closed above it and reopened for a brief conditional statement php tidbit and then closed again before the closing </style> tag. The reason the php is reopened beneath the </style> and above the } is because the function you're righting function wpt_style_header() IS php.

If you didn't have php opened up again before the } closing of the function, then it'd fail because the whole of the function wouldn't have been in php. Hopefully this rambling made some sense and helps somebody :)