Layers Plugin Guide: Setup Your Main Plugin File

Extension Development

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

The first task in creating a Layers Extension is to think about what the Plugin will do, and come up with a unique, but semantic name. Always check the existing Layers Extension list (Layers → Marketplace) and other repositories to verify that your name is unique; you might also do a Google search on your proposed name. If you are creating a Layers version of an existing plugin, it is best to use the original plugin title coupled with “Layers”. Example, VideoBox for Layers

Creating a Plugin from scratch is fairly simple. It is the functions you intend to add and the widget construction that bring the complexity. Starting with a file framework like the one below, or downloading our example plugin should help simplify the process.

  1. Create a folder with a lower-case name, prefixed by layers-. Use hyphens to separate multiple words. In this example, we call our extension “layers-demo-extension”:
  2. Create a new PHP document and save it inside this folder as functions.php, index.php or alternatively name it the same as your plugin, ie “layers-demo-extension.php”
  3. Create an assets folder to hold any front-end stuff such as CSS, images, or scripts.
  4. Create an includes folder for your plugin class file and any supporting PHP files or libraries.
  5. Create a lang folder if your plugin will use its own text domain to manage translations
  6. If you plan to introduce widgets in your plugin, you may also have a widgets folder.
  7. Create a blank plain-text file for your readme and changelog. Using markup and the md extension allows these files to integrate with Git and can be opened by normal text editors on the local computer. We will cover what these files should contain at the end of the guide.

You would now have a very basic skeleton for your plugin with the following structure:

Your main plugin file (functions.php, index.php or your-plugin.php) is where the majority of your custom functions and hooks will reside and is also how your plugin is recognized and registered in WordPress.  A basic wireframe of this file looks like this:

Header

The file header , which is all the info contained by asterisks, provides identifying information for your plugin and should include what is shown above for any Layers Extensions.

The absolute minimum information WordPress needs to recognize your Plugin is the Plugin Name line. The rest of the information (if present) will be used to create the table of Plugins on the Plugin management screen. The order of the lines is not important. Below is a quick overview of the elements shown in our example above:

  • Plugin Name: The name of your plugin as you want it to appear in the Plugin management screen
  • Version: Your first release will typically be 1.0
  • Plugin URI: The url to where users can view info about the plugin
  • Description: What does your plugin do for Layers?
  • Author: Your name as you want it to appear in the Plugin listings
  • Author URI: Your website or url to the plugin website, appears in the Plugin listings.
  • Requires at Least: This info is optional, but helpful if your extension needs a specific version of WordPress to support functions or hooks it uses.
  • Tested Up To: Usually the version of WordPress you are running during development, or a future release if you choose to test your plugin against a release candidate
  • Layers Plugin: Should be true if your extension needs Layers to work.
  • Layers Required Version: Used to help notify users if they need to update Layers before using your Extension. Set to the minimum required version.
  • Text Domain: a unique text domain that allows any visible text elements to be translated in their own language file.
  • Domain Path is optional. Specify a path if the translations are located in a sub-folder. Example: if you have a “lang” folder setup to hold the default .po and .mo file for translation plugins to use, the Domain Path will be /lang/ and must have the first slash.

License

At some point within or after your file header, you need to include the GPL2 License URI or a full license text blurb as shown here:

You’ll want to block direct access to your plugin PHP files by adding the following line at the top of each of them on the first line of code (ie after the comment section is closed off)

 Save your file in the root of your new plugin folder.

Important: file must be in UTF-8 encoding!

For a detailed overview of plugin header information, refer to the WordPress Codex: Writing a Plugin section. Please note the Codex tutorial is worded specifically for writing WordPress plugins intended for the .org repository and may not pertain to your project.

Constants act as placeholders for commonly repeated information and do not need to be globally declared. These are optional, but can reduce edits and mistakes and are referenced extensively throughout this guide. Here we define 6:

LAYERS_DEMO_EXTENSION_SLUG
= Set to a simple text string that will be used as your text domain or wherever the theme slug needs to be referenced.
LAYERS_DEMO_EXTENSION_VER
= Your plugin’s version. Don’t forget to update this every time you release a new update of the plugin!
LAYERS_DEMO_EXTENSION_DIR
= Returns the absolute path to the plugin folder. See plugin_dir_path
LAYERS_DEMO_EXTENSION_URI
= Returns the fully qualified url to the plugin folder. See plugin_dir_url
LAYERS_DEMO_EXTENSION_FILE
= Returns the path to the plugin file
LAYERS_REQUIRED_VERSION
= Set to the minimum required Layers version your plugin needs. We will use this later when setting up version check notifications.

We use trailingslashit on these path functions to ensure that any trailing slashes are removed, and then only one is applied to the end of the path or URL. This helps avoid accidental double-slashes.

You should now have a bare-bones project setup with a populated functions file which can be activated in WordPRess. Zip up the folder, then give it a test-run by uploading it to the PluginsAdd New screen and activating it.

listing

Installing the Plugin

  1. Ensure Layers is already installed!
  2. Navigate to Plugins and click  ADD NEW
  3. Click UPLOAD Plugin.
  4. Click Browse and choose your zip, then click INSTALL NOW.
  5. Once it is installed, click Activate.

 

If you’ve activated just a bare functions.php, you should get a success message, but your plugin isn’t doing anything useful yet. In the next part of this guide, we will cover each section of the functions file in detail.

View example of  full demo extension functions file