I am new at WordPress themes development, and I am willing to build a complete theme, while I still at the index.php, I found that the file itself becomes more complicated, a lot of HTML tags comes interleaving with PHP code and loops, I think it would be difficult to remain at the structure, so is there any defined structure to make the code more organized and editable at the feature?

1 Answer 1


It seems like the Wordpress community really loves this mix of PHP and HTML together, and as you say it often leads to very hard to understand and maintain code.

It is a common programming pattern to separate out 'logic' and 'presentation'. Logic here is PHP code which makes decisions, presentation is HTML.

I strongly recommend trying to separate the two whenever you can, and put as much PHP in one place, and as much HTML in another place.

For example you might have:

<b>Your name: <?php if user_logged_in() { $name=get_user_name(); echo $name; }  else { echo "please log in!"; } ?></b>

To separate that out you could write:

      if (user_logged_in()) {
            $name = get_user_name();
       } else { 
            $name = "please log in!";

<b>Your name: <?php echo $name ; ?></b>

This approach allows you to begin to move all your PHP and 'business logic' away from the HTML and the 'presentation' so that you can change the logic of the way things work separately from the way it's presented.

I hope this helps! There are even more advanced solutions like PHP templating engines which do the separation even better, but maybe this is overkill.

  • your example inspires me a lot, thank you :D Jun 23, 2020 at 14:11

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.