Custom Special Pages Example - lokothodida/DM_Matrix GitHub Wiki

This is a brief walkthrough on how to set up custom pages utilizing your own custom fields. This is by no means a replacement for the existing GetSimple Custom Fields or Special Pages plugins, but merely an example of what can be done using The Matrix. Ideally, one would modify this example to suit a very specific need, and also to further understand how best to optimize their use of the plugin. This example necessitates a hook to the 404 error page, which is best achieved either through having your own plugin file or with components using Shawn_A's Component Hook plugin.

Data structure

Build a table with the following properties

  • Name: pages
  • Fields:
    • Name: title, Type: textlong
    • Name: slug, Type: slug
    • Name: tags, Type: tags
    • Name: template, Type: template
    • Name: parent, Type: parent
    • Name: date, Type: datetimelocal
    • Name: content, Type: wysiwyg
  • Max Records: 0

Class (presumably in a separate file, e.g. 'dummy_custom_page/classes.php')

<?php
  class DummyCustomPage {
    /* method called on 404 hook to display page content
     */
    public function showPage() {
      // initialize global variables and The Matrix
      global $id, $data_index;
      $mat = new TheMatrix;
      $page = $mat->query("SELECT * FROM pages WHERE slug = '$id'", "SINGLE");

      // display the details if $page produces an array
      if (!empty($page)) {
        $data_index->title    = $page['title'];
        $data_index->date     = $page['date'];
        $data_index->metak    = $page['tags'];
        $data_index->meta     = '';
        $data_index->url      = $page['slug'];
        $data_index->content  = $page['content'];
        $data_index->parent   = $page['parent'];
        $data_index->template = $page['template'];
        $data_index->private  = '';
      }
    }

    /* admin panel
     */
    public function admin() {
    }
  }
?>

Plugin file

<?php
  # load class
  require_once(GSPLUGINPATH.'dummy_custom_page/classes.php');

  # instantiate class
  $dummy_custom_page = new DummyCustomPage;

  # register plugin
    register_plugin(
      $thisfile,
      'Dummy Custom Page',
      '',
      '',
      '',
      '',
      'plugins',
       array($dummy_custom_page, 'admin')
    );

  # actions/hooks
  add_action('error-404', array($dummy_custom_page, 'showPage'));
?>

Adding custom fields

Adding extra fields can be done just as you would add any other Matrix table field. Implementing the field is slightly trickier. The below modifications to the class will solve the problem. We modify the class to load the content structured as we specify (in a template file in your 'dummy_custom_page/' directory) then create the template.

  • Example of extra fields:
    • Name: category, Type: dropdowncustom
    • Name: author, Type: users

Modification to class

  if (!empty($page)) {
  ....
    // start output buffering to store template file
      ob_start(); 
      include_once(GSPLUGINPATH.'dummy_custom_page/template.php');
    // load content from buffer and store in variable
      $data_index->content = ob_get_contents();
      ob_end_clean();
  ...
  }

Template file for content

  <div class="published">
    Published by 
    <span class="author"><?php echo $page['author']; ?></span> in 
    <span class="category"><?php echo $page['category']; ?></span>
  </div>
  <?php echo $page['content']; ?>
⚠️ **GitHub.com Fallback** ⚠️