Simplifying repetitive tasks with the Codeless library

How do you deal with daily repetitive tasks such as add_action(), 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

$helper::show_admin_notice( $content = 'The message' , $type = 'success' );

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.

$helper::show_admin_notice( $content = 'The message' , $type = 'success', $id = 'test_notice' );

Modal interfaces

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:

<script>

  jQuery(document).ready(function ($) {

    jQuery( '.trigger' ).click(function() {

      var popup = null;

      popup = codelessUi.popup()
        .modal( true )
        .size( 740, 480 )
        .title( 'Window title' )
        .content( '.popupcontainer' )
        .show();

    });

  });

</script>
<a href="#" class="trigger">Trigger</a>

<div class="popupcontainer" style="display:none">
  Yo!
</div>

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:

  1. Child Theme
  2. Parent Theme
  3. 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:
<?php
define( 'RESTAURANT_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );

class Restaurant_Template_Loader extends \TDP\Plugin_Template_Loader {

  // Prefix for the filters within the class.
  protected $filter_prefix = 'restaurant_plugin';

  // Path to the plugin.
  protected $plugin_directory = RESTAURANT_PLUGIN_DIR;

  // Name of the folder that contains all the templates within the plugin.
  protected $plugin_template_directory = 'templates';

  // Name of the folder that contains the templates within a theme.
  protected $theme_template_directory = 'restaurant-templates';

}

$restaurant_template_loader = new Restaurant_Template_Loader;
Load the template file:

To load a template file you can now access the get_template method:

<?php
$restaurant_template_loader->get_template( 'file.php', array( 'my_variable' => 'value' ) );

You can pass arguments via an array to template files and then access the data in it echo $my_variable.

Checking overwritten files:

If you wish to check which files have been overwritten by a theme, you can access the get_overwritten_template_files method:

<?php
$restaurant_template_loader->get_overwritten_template_files();

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.

<?php
/**
 * Template file comments here...
 *
 * @version 1.0.0
 */

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 ;)