Layers Plugin Guide: Setup Your Plugin Class

Extension Development

Last Updated: for: 1.5.4 Time to Read: 3 minutes

This guide includes several code examples which use a placeholder callback, variable or ID such as layers_demo_extension. Do not copy/paste code directly from this guide into your project without changing these values to be unique to your project!

In the next section we will cover instantiating your plugin class, which includes most hooks and public functions, as well as adding common functionality like widgets, post types, shortcodes and scripts. This guide will walk you through using Object Oriented Programming to organize your hooks and functions in a more efficient and reusable way via a plugin class.

Include the Class

If your plugin will be hooking into any WordPress or Layers filters or actions, a core class should be created to help keep your code organized, efficient and reusable.

To execute everything in your class, you need to include it in your main plugin file (ie functions.php), and instantiate it. You may also have other helper files or libraries which can be included this way, but don’t use require_once   for scripts or styles – those will be included via your class.

Create an empty PHP file in your includes folder now to set the file name with the following syntax: class-layersyour-extension-name.php

Add your file to the main plugin file using the internal path with require_once  :

Your Class opens with a simple header and a unique class name. Class names are typically case-sensitive and capitalized on the first letter. To keep things consistent, begin your class name with Layers , and end it with your Extension’s name. In this example, our extension is called Demo Extension. Make sure yours is something unique.
class Layers_Demo_Extension{}
Keep in mind Classes_Are_Always_Capitalized

The first thing we do inside our class is setup the instance and initiate it. The get_instance()   function creates a singleton class that’s cached to stop duplicate instances.

Next, we setup a constructor to hold our hooks and custom functions.

PHP classes have a constructor function, __construct   we can use here.

All WordPress and Layers actions and filters (hooks) will be registered under this constructor, which is executed when a new instance of the class is instantiated from our functions file.

init() can be used in place of __construct() but is not as sensible or necessary. If init() is used, be sure it is referenced on line 3 of the get_instance function.

Below is an example of a group of hooks for the functions we will be creating to localize, setup some page templates, create some customizer controls, and enqueue our scripts and stylesheets:

The remainder of your class will consist of the functions utilizing the hooks and filters registered in your constructor. These might include:

  • Localizing your plugin via plugins_loaded
  • Adding Template Locations via layers_template_locations
  • Adding or modifying customizer controls via layers_customizer_controls
  • Adding Sidebar locations
  • Adding option panel links to the Layers Admin menu
  • Enqueing scripts or stylesheets via admin_enqueue_scripts or wp_enqueue_scripts
  • Registering widget files

These will be covered in detail later on in this guide. To give you an idea of how these look, let’s localize the plugin:

Your first custom function allows translation of any text strings you may have in your plugin. You must have a lang folder in your plugin structure to hold the .mo file. We will cover how to generate this file for your plugin during the testing phase of this walkthrough.

Reference: load_plugin_textdomain() and i18n

When you are ready to give your plugin a go with your new plugin class, switch to your main plugin file (ie functions.php or index.php) and create a new custom function i.e. layers_demo_extension_init() . This new instance of your class is registered using the new   keyword and should be placed right after the require_once()  call to the class file.

Line 1: Setup your custom function

Line 5: Set the global variable to your class name’s instance

Line 8: Hook your function to plugins_loaded()  }

Next up, how to include scripts and stylesheets →