How do you deal with daily repetitive tasks such as
add_filter(), admin notices, etc.. in your WordPress projects? Probably by manually rewriting the same things over and over again like I did. Wouldn't it be nice if you could reduce the amount of code you had to write for those tasks?
Well now you can! Introducing wp-codeless-lib. An helper class with functionalities that are common within my WP projects.
Here's a quick overview of the main features of the class, or you can go ahead and see example usage of each functionality.
Dismissible admin notices
Admin notices are an easy way to display a message at the top of admin pages in WordPress. However it's quite annoying to hook into
admin_notices and create the html markup.
With the Codeless library, showing admin notices it's easier. The markup is taken care of by the class. Furthermore you can also create dismissible admin notices.
A dismissible admin notice is a notice that once dismissed, won't display again.
Creating a normal notice
Creating a dismissible admin notice
By simply adding a third parameter to the
show_admin_notice, the notice will be dismissible. When dismissed, the notice won't show again. All of this is done through ajax.
I've always been a fan of the media manager ui in WordPress and I'm also a big advocate of using the WordPress UI when building plugins. The Codeless library comes with a simple jQuery script that you can use to recreate modals with the same interface as the media manager.
The following script is self explanatory:
Ps: don't use inline css, this was just an example.
Plugin template loader
Template files are standard for themes. For example if a user wishes to customize a template file he/she can just copy and file into the child theme and modify it. Unfortunately this isn't possible with plugins unless developers build their own custom template loader.
Many plugins like WooCommerce and EDD have their own template builder so that developers can customize the look of plugin.
The Codeless library comes with it's own template loader that can be reused as many times as you want. It was highly inspired by the WP Job Manager plugin, and WooCommerce.
The template loader into the Codeless library is also capable of recognizing which template files are being overwritten by a theme and if the template file into the theme is outdated.
The following is the order with which template files are loaded:
- Child Theme
- Parent Theme
- Plugin’s folder
Here's an example of how to user the template loader.
Extend the template loader class and configure it for your plugin:
Load the template file:
To load a template file you can now access the get_template method:
You can pass arguments via an array to template files and then access the data in it
Checking overwritten files:
If you wish to check which files have been overwritten by a theme, you can access the
The method will return an array with details about the files overwritten and whether or not the file is outdated.
If you wish to use this feature, your template files must have the following comments at the top of the file. You must define @version.
This was an overview of the main features of the Codeless library. More features and examples are available on github.
Hope you find this library useful ;)