12

When doing a template such as single.php and you have php wrapped in html, is it best to :

  1. Start + Stop PHP? for example

     <h1 class="post-tilte"><?php the_title(); ?></h1>
     <p class="post-content"><?php the_content();?></p>
    

Or

  1. Echo HTML and Escape PHP? For example -

    <?php echo '<h1 class="post-title">' . get_the_title() . '</h1>
    <p class="post-content"' . get_the_content() . '</p>
    

I dont have a prefreed choice and find myself doing both, just curious to hear some thoughts

2
  • 6
    The first method is more designer friendly. So when in templates with a lot of HTML and little bit of PHP, do the first one. The second one is useful when there is a lot of PHP and a little bit of HTML. Do it in functions.php or plugins etc.
    – Fayaz
    Commented Feb 28, 2017 at 14:19
  • 3
    You forgot there's also printf( '<h1 class="post-title">%s</h1>', get_the_title() );
    – Howdy_McGee
    Commented Mar 2, 2017 at 15:34

5 Answers 5

10

That's question is only relevant, because WordPress use a mix from a coding language and layout language. If you would use a template language, syntax, than is this topic not relevant. But to your question. If you use your example source for a Theme, much more layout language like html, then I prefer the first one - it is much more readable for designer and users, there must read the markup. It is easier to create an overview about the markup, have you the open and closing tags etc.

For the include in plugins, code with more logic and flow is the second example easier to implement. The main topic is php, not markup and this should it visible in the source. That's also a point to think about to exclude this markup in template files and separate the markup from the logic.

1
  • 1
    Another benefit of the first one is that certain editors will highlight the corresponding opening or closing tag if you place your cursor in it but the latter will usually break that convenience.
    – MonkeyZeus
    Commented Feb 28, 2017 at 19:16
8

Just as additional information. WordPress has handbooks for PHP & HTML on best practice regarding coding standards, also for CSS, JavaScript and Accessibility. You might find them helpful for getting deeper into the matter.

5

As explained in above answer, the first method is designer friendly & second method maybe suitable in cases of plugins and complex php codes where the number of html tags are only few.

But most of WordPress template tags has before and after parameters and its more appropriate to use your html codes inside of function call.

for example in the case of the_title it has three parameters

the_title( $before, $after, $echo );

you can pass your html to before and after parameters like below

the_title( '<h2 class="title">', '</h2>', true );

the advantages of this method is

  • Designer Friendly
  • will not print out html tags if title is empty
  • can be used in complex php codes as well as in simple template files
0

The first method is preferable because:

  • It clearly separates the fixed HTML layout from the dynamic contents.
  • It is easier to read.
  • It is better supported by syntax highlighting in most editors, because the in-string html attributes are usually not recognized.
  • It prevents you from intermingling HTML and PHP too easily.

Because of the reasons stated above, the first method leads to code that is easier to understand and thus easier to maintain.

In my experience, in some situations, it can appear to be more convenient to use the second method. However, that is usually due to weak (software) design and should be seen as warning sign.

0

You're code example is marginal because it only shows two lines of code, but I believe the overall answer is quite situational. If you are using a PHP line within an HTML block use the first method. If you are using an HTML line within a PHP block go with the second one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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