Some of the best practice to create a new WordPress custom plugin,
- Name your plugin based on what your plugin is going to do. Don’t name it with your pet name or your own name, should be unique and meaningful. You can better search your name here (http://wordpress.org/extend/plugins/) to get the unique name.
- Create a folder which will have all necessary files, scripts and images. The folder name should be same as the plugin name. Don’t use space, underscore in folder name, but you can use hyphen
- Always prefix with unique name for variables, function. So that it will not conflict with other plugins function.
Good Practice: bufffercode_plugin_name() ; $buffercode_variable_name
Bad Practice: plugin_name(); $variable_name
- Always keep plugin main file and uninstall file in root directory and keep other files in sub directories
Standard folder structure for a WordPress plugin:
- /unique-plugin-name – (no spaces or special characters) [eg. buffercode-plugin]
- unique-plugin-name.php – Primary plugin PHP file [eg. buffercode-plugin.php]
- uninstall.php – The uninstall file for your plugin
- /css – Folder for style sheet files
- /includes – Folder for other PHP includes
- /images – Folder for plugin images
The Primary plugin file should have this header structure to give detailed information about the plugin and author information.
<?php /* Plugin Name: Your Plugin Name [eg. buffercode-plugin] Plugin URI: http:///your-plugin-address/ [eg. http://buffercode.com/] Description: Give brief description about your plugin Version: 1.0 Author: Your Name [eg. BufferCode] Author URI: http://author-url/ [eg. http://buffercode.com/] License: GPLv2 */ ?>
Using plugin_dir_path( __FILE__ ) we can get the direct plugin path address.
If we give
<?php echo plugin_dir_path( __FILE__ ); echo plugin_dir_path( __FILE__ ) .’js/scripts.js’; ?>
This will output
Some of the default WordPress function to get the URL information
- plugins_url() — Full plugin directory URL (for example, http://buffercode.com/wp-content/plugins)
- includes_url() — Full includes directory URL (for example, http://buffercode.com/wp-includes)
- content_url() — Full content directory URL (for example, http://buffercode.com/wp-content)
- admin_url() — Full admin URL (for example, http://buffercode.com/wp-admin/)
- site_url() — Site URL for the current site (for example, http://buffercode.com)
- home_url() — Home URL for the current site (for example, http://buffercode.com)
Confused by seeing site_url and home_url?
Here is the explanation. Site_url() will give your exact blog address and home_url will give you the domain address.
Consider this example, we have hosted our wordpress blog in http://buffercode.com/blog/index.php
Site_url returns => http://buffercode.com/blog
Home_url returns => http://buffercode.com