skip to Main Content

This is for the wordpress developer out there whos been doing this for a long time, what i want is a suggestion on how to structure my code, what i want is a "separation of concern". Here is my set up right now, I have a page template that is a form, and i want a separate file to abstact the code from the template page. What im after is a maintainable code base.

Can you guys give me an suggestion, on how would you structure your code base, in a maintainable way. Any suggestion would be appreciated.

2

Answers


  1. You’re looking for get_template_part()

    Loads a template part into a template.
    Provides a simple mechanism for child themes to overload reusable sections of code in the theme.

    Parameters Description
    $slug (string) (Required) The slug name for the generic template.
    $name (string) (Optional) The name of the specialised template. Default value: null

    In your case we could place your form in a template part, eg: custom-page-form.php then on the front end call it via <?php get_template_part( 'custom-page-form.php' ); ?>.

    Login or Signup to reply.
  2. If you’re looking for kinda MVC, I would suggest you to use Timber. It provides twig template engine to handle your views, while logic behind it (models, controllers, config, etc) can be organized anywhere (but the endpoint must be functions.php).

    Also, index.php must be present in theme, so it could be a start point for rendering your views. In fact, it could be any general WordPress template, but I prefer to keep only index.php.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search