WordPress Plugin Skeleton / Base
During the course of my work, I have had to write numerous plugins. I came to a point where I wanted some base code that I could just copy and run with for any need.
I have learned that it is best to separate front end code from administrative code, when possible. This code segregation provides security and decreases code base as only code that is needed is loaded. I also feel that it is in best practice to make use of Object Oriented programming and autoloading via a PSR-4 autoloader. Thanks Composer! People in the Advanced WordPress group on Facebook were looking for an example class to present a REST endpoint. This is included in the example.
All of the code for the plugin is available on GitHub. Feel free to submit a pull or fork request.
How to make this code your own:
- Download it!
- Open your Plugin.php file to edit the header and set your namespace.
- Rename the folders under Assets to match your new namespace. For example, if your new namespace is Company\Plugins\Telekinesis, you would want to rename the path Assets\Crumbls\Plugins\Skeleton to Assets\Company\Plugins\Telekinesis. Autoloading looks at the namespace to know where to load files.
- Inside of the new namespaced folder, you want to edit your Admin.php and Plugin.php to have the appropriate namespace, such as Company\Plugins\Telekinesis again. They should match the same namespace you used in the Plugin.php file.
- If you would like to view an example of the Rest endpoint, inside of the nested Plugin.php file, uncomment the line that says new Rest();
- Learn from it. If you have any comments, questions or ideas, let me know in the comments form below.