Layers Plugin Guide: Setup Your Main Plugin File
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
Create the Folder
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.
- 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”:
- 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”
- Create an assets folder to hold any front-end stuff such as CSS, images, or scripts.
- Create an includes folder for your plugin class file and any supporting PHP files or libraries.
- Create a lang folder if your plugin will use its own text domain to manage translations
- If you plan to introduce widgets in your plugin, you may also have a widgets folder.
- 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:
Main Plugin File
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:
* Plugin Name: Layers - Demo Extension
* Version: 1.0
* Plugin URI: http://www.yourpluginpage.com
* Description: What does your Extension do for Layers?
* Author: You
* Author URI: http://www.yoursite.com/
* Requires at least: 4.5
* Tested up to: 4.5.3
* Layers Plugin: True
* Layers Required Version: 1.5.4
* Text Domain: layers-demo-plugin
* Domain Path: /lang/
if ( ! defined( 'ABSPATH' ) ) exit;
// define constants (optional)
// Load plugin class files
// Instantiate Plugin
// Additional Functionality
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.
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:
/* Copyright YEAR YOUR NAME (email : PLUGIN AUTHOR EMAIL)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Make it Secure
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)
if ( ! defined( 'ABSPATH' ) ) exit;
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:
define( 'LAYERS_DEMO_EXTENSION_SLUG' , 'layers-demo-extension' );
define( 'LAYERS_DEMO_EXTENSION_VER' , '1.0' );
define( 'LAYERS_DEMO_EXTENSION_DIR' , trailingslashit( plugin_dir_path( __FILE__ ) ) );
define( 'LAYERS_DEMO_EXTENSION_URI' , trailingslashit( plugin_dir_url( __FILE__ ) ) );
define( 'LAYERS_DEMO_EXTENSION_FILE' , trailingslashit( __FILE__ ) );
define( 'LAYERS_REQUIRED_VERSION' , '1.2.4' );
= Set to a simple text string that will be used as your text domain or wherever the theme slug needs to be referenced.
= Your plugin’s version. Don’t forget to update this every time you release a new update of the plugin!
= Returns the absolute path to the plugin folder. See plugin_dir_path
= Returns the fully qualified url to the plugin folder. See plugin_dir_url
= Returns the path to the plugin file
= 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.
Install & Activate
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→ screen and activating it.
Installing the Plugin
- Ensure Layers is already installed!
- Navigate toADD NEW and click
- Click UPLOAD Plugin.
- Click Browse and choose your zip, then click INSTALL NOW.
- 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.