Modularity Documentation


Intro

Modularity provides a simple but effective methodology to empower any WordPress-Developer to develop clean, modular, maintainable, shareable and extendable code even on larger projects.

Modularity does not force you to work with a specific frontend-framework and will not lock you in a complex library.

Modularity helps you and your team to share the same way of writing code for WordPress-Projects.

You can start working modular on every existing or new project. Modularity loads, compiles and enqueues your theme-modules automatically. No local transpiling needed. Start coding straight away!

Notice: The code examples below will encourage everyone to write lightweight code, which is easy to write and to understand. Let’s keep things simple.

Get started

  1. Download and install the free and Open Source Modularity Plugin here
  2. Create a folder called /modules in your theme’s root folder
  3. Thats it! You can now start to develop your own modules

01 . Anatomy of a module

Example 1 (simple)

/your-theme-folder
  /modules
    /module-xyz
      module-xyz.php - main module file - is loaded automatically
      module-xyz.template.php - a template file - can be included
      module-xyz.scss - module styles - compiled and enqueued automatically
      module-xyz.js - module scripts - compiled and enqueued automatically

Example 2 (complex)

/your-theme-folder
  /modules
    /module-xyz
      /assets - assets like static images, fonts, media
      /submodules - can hold submodules that extend the main module
      /templates - holds the multiple template files of a module
      module-xyz.php - main module file - is loaded automatically
      module-xyz.scss - module styles - compiled and enqueued automatically
      module-xyz.js - module scripts - compiled and enqueued automatically
      readme.md - module infos on how to use it and changelog

Hints

02 . PHP code structure and naming

Example module-xyz.php

<?php 

namespace YourMainNamespace\ModuleXyz;

defined("ABSPATH") or die;

add_action('init', 'YourMainNamespace\ModuleXyz\register_blocks');
add_action('modulexyz/email', 'YourMainNamespace\ModuleXyz\send_email');

function register_blocks()
{
 // your code to register blocks here
}

function send_email()
{
  // do some functionality
}

Hints

03 . SCSS structure and naming

Example module-xy.scss

.module-xyz {
  // modules styles
}

Hints

04 . JS structure and naming

The modular architecture of your further websites will lead to clear code over several modules. As main WordPress development is still done via serverside PHP-scripts, the JavaScript part of Websites and Web-Applications is comprehensible. Because of that we do not need (so far) a way to transpile ES5 (or later) JavaScript.

We recommend to just start with simple VanillaJS and/or jQuery for frontend-scripting. A typical structure for your module`s JS could be:

Example module-xy.js

function($) {
  $(document).ready(function(){
    // function calls here
  });
  
  // functions here
}

Hints

05 . Loading, compiling and enqueing

Ok nice but what is the Modularity Plugin doing for me?

If modules are present in the given structure (01), Modularity will:

  1. load all modules+submodules main .php files
  2. compile every SCSS file to a CSS with same name in same folder
  3. enqueue all module styles and scripts the right way

Modularity is doing all this for you! Automagically!

Footnote

This documentation is a basic start. We will go on to provide more info about Modular Development.

Modularity is not new. We are now at v5 so we already had some iteration rounds. 🙂