0

I've just started making my own WordPress widgets and don't have a very good understanding of good practices with PHP or HTML. To add html to a widget I have seen two different methods, the first being inside the PHP using echo and the second being closing off the PHP with ?> , writing some HTML then reopening the PHP with a <?php tag.

For my widget I have found it much easier to use the second method but I am concerned about whether doing this is bad practice and could lead to problems later on. Is this something that just happens to work and wasn't intended to be used this way so could be broken in future updates?

1
  • 1
    No, it's not bad, WordPress core also does that in the widget callback function. So you can use any methods you see fit for a specific part in your code.
    – Sally CJ
    Jun 13 '20 at 16:05
2

"Practice" is a matter of personal preference. The opening and closing PHP tags (<?php and ?>) are there as a matter of convenience. Basically, try to write your code in context. In example,

// PHP code here
?><a href="<?php the_permalink() ?>"><?php the_title() ?></a><?php
// PHP code here

looks (to me) like HTML code plopped right in the middle of a PHP code block (like in a function of the functions.php file), so I will write the display of this HTML link in that context as

// PHP code here
echo '<a href="'. get_the_permalink() .'">'. get_the_title() .'</a>';
// PHP code here

But, if I am in a HTML code block (like in the single.php file of a template), I will write the display of this link in HTML context (as opposed to PHP context above). So, I will not use

// HTML code here
<?php echo '<a href="'. get_the_permalink() .'">'. get_the_title() .'</a>'; ?>
// HTML code here

but instead will code the display of the link as

// HTML code here
<a href="<?php the_permalink() ?>"><?php the_title() ?></a>
// HTML code here
0

It's good practice to try and separate out code and presentation as much as possible. The best way to achieve this is to keep all your HTML in a 'template' and all your code in a PHP file somewhere.

In practice, lots of PHP developers don't do this, and you get a mix of code and HTML which is often hard to read.

I would suggest wherever you can, put all your PHP in one part of the file, and then drop the results of the PHP into the HTML using as short pieces of PHP as possible. Here's an example:

<?php
// do some complicated stuff here and keep all the code logic in one place:
$a = complicated_function();
$b = work_something_out();
$c = $a + $b;
if ($c = "hello") {
   $d = "this";
}
// etc etc
?>

<!-- now put all that in your HTML using shortest PHP tags possible -->
<div><p><?php echo $a; ?></p></div>
<h1><?php echo $b; ?></h1>
....
<div class="<?php echo $c; ?>"><?php echo $d; ?><?div>

So here the code and the HTML are as separated as possible and it's much easier to see what the code is doing in one place and what the presentation is doing in the second place

If you have a lot of PHP, pull it out into a separate class / file as much as possible.

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.