Create a Simple Module

Let's create a "Latest News" Module to display a banner at the top of every page.

  1. Create a /modules/ subfolder in your Application directory:

    • ~/lawpress-app/modules/
    • (For this step and the ones that follow we will use ~/ to replace ~/Sites/LawPress/www/wp-content/mu-plugins/)
  2. Create a subfolder for your actual Module:

    • ~/lawpress-app/modules/latest-news
  3. Create a Module Main File:

    • ~/lawpress-app/modules/latest-news/latest-news.php
      • The folder name and file name must match.
    • Then add the following code:

      <?php
      class _LawPress_Latest_News extends WPLib_Module_Base {
          static function on_load() {
              self::register_helper( __CLASS__, 'LawPress' );
          }
          static function the_latest_news_html() {
              ?>
              <div id="latest-news" style="background:red;text-align:center;color:white;">
                  <?php echo 'This is your Latest News!'; ?>
              </div>
              <?php
          }
      }
      _LawPress_Latest_News::on_load();
      
  4. Register the Module in the Application:

    • In ~/lawpress-app/lawpress-app.php add the following method:

      static function on_load() {
          self::register_module( 'latest-news' );
      }
      
    • At the end of ~/lawpress-app/lawpress-app.php add this call:

      LawPress::on_load();
      
  5. Document _LawPress_Latest_News as a Mixin in the Application:

    • Add this PHPDoc header to ~/lawpress-app/lawpress-app.php on the line above the class declaration (class declaration shown for context):

      /**
       * Class LawPress
       *
       * @mixin _LawPress_Latest_News
       */
      class LawPress extends WPLib_App_Base {
      
  6. You now have a simple Module that you can use in your Theme.

    • You should test your site to make sure it loads without breaking, though you will not see any changes yet.

What Did We Just Do?

We created a simple module whose Main file had a Main class _LawPress_Latest_News with an output method the_latest_news_html() and we registered the Module in the Application so that the Application will load the Module and make its functionality available.

Notes:


NEXT: Use the Simple Module in a Theme Template

BACK: Create a Theme Class

UP: QuickStart