WordPress Plugin Library

Installation

You have a couple of options to download it. I’ll list them below.

git

To install WordPress Plugin Library with git you just need to have git installed and to open a terminal window (or Windows Terminal if you are on Windows).

Navigate to the folder where you have the WordPress instance to the plugins folder. The path would look something like this:

/wordpress-folder/wp-content/plugins

And when you are in the plugins folder you have to enter the following snippet in your terminal:

git clone https://github.com/sorinmarta/wp-plugin-library

You are done! Now you should have a new folder that’s called ‘wp-plugin-library’ that contains the files of the library.

GitHub

My less favorite option is also very simple to use. You just have to check my GitHub repo with the WordPress Plugin Library.

And while you are on that page you have to click on “Code” and “Download as ZIP”.

Usage

After downloading the library files you should have the following file structure:

app
 - controllers
  - index.php

 - helpers
  - index.php
  - wppl-helper.php
 
 - lib
  - index.php
  - wppl-form.php
  - wppl-loader.php
  - wppl-view.php

 - models
  - index.php

 - views
  - index.php

Most of the file names talk for themselves so I think you already have an idea of what they are doing. But let’s take the functions one by one and see what they are doing.

Also as you can probably see from the naming convention all the library files and classes have the ‘WPPL’ prefix so that it avoids naming conflicts and also respects the WordPress coding standards.

Loader

The WPPL_Loader class is simply a place where you can require all your files using the structure that is in place.

Helper

The WPPL_Helper class is a helper class that’s going to have helper static methods for various usages.

dd

The ‘dd ‘ name stands for ‘die and dump’ which basically means that it will dump the parameter that it is given using the ‘var_dump’ native function of PHP and then it will end the process so that nothing that comes after it will run.

Example
$value = 'Man, I really need to find out what I am';

WPPL_Helper::dd($value);
Response
string(40) "Man, I really need to find out what I am"

redirect

The ‘redirect ‘ method is helping with redirects of any kind. It can pass over messages and it is integrated with the WPPL_View class to also display the notices as either errors, success messages, or even regular notices.

Required Parameters
  • URL (required) – The location where the user should be redirected to
  • Type – The type of the message. Can be ‘error’, ‘success’ or ‘alert’
  • Message – The string it should display as the message
Example
WPPL_Helper::redirect('https://huskystudios.digital', 'success', 'Sending some love 💛');
Response
The user will get redirected and they will see a green notice with the message value 🙂

View

The WPPL_View class is a class inspired by Laravel’s ‘view’ function that helps you retrieve a view anywhere in the application.

The class constructor demands the name of the view file name when you are trying to create a new instance. The file name can also be a path if you have views in separate folders.

Important: The class is going to be looking for file names and paths that are inside the app/views folder of the plugin and it already has the ‘.php’ suffix so you don’t need to include it.

Example
// The view file - app/views/settings.php

<h1>My awesome plugin is here!</h1>
<p>This is where I'm going to add my badass plugin's settings</p>
new WPPL_View('settings');
Response

My awesome plugin is here!

This is where I’m going to add my badass plugin’s settings!

Form

The WPPL_Form class is a bit more complicated than the other basic classes. It creates forms that can be directly processed using WordPress hooks for admin_post.

To create a new form you need to create a new instance of the class and pass a few values to the constructor. You can see more details below.

Methods

__construct
Required Parameters
  • Action – The name of the action suffix that will be expected in WordPress
  • Nonce – The value of the nonce that is going to be used for the security verification
  • Inputs – An array of arrays that will configure the form elements
Inputs

The input has a few argument values that can be passed to the class.

  • Element (required) – The attribute is the attribute that sets the type of HTML node needs to be created. Which can be an input element or a select element
  • Type (required) – The type of the input (or select, or dropdown)
  • Name (required) – The name attribute of the HTML element
  • ID (required) – The id of the HTML element
  • Placeholder – The placeholder attribute of the HTML element
  • Value – The value attribute of the HTML element
  • Class – The class attribute of the HTML element
Selects

The selection also has an argument that is required and a few optional arguments

  • Options (required) – An array of option elements (they are described below)
  • Name (required) – The name attribute of the HTML element
  • ID (required) – The id of the HTML element
  • Class – The class attribute of the HTML element
Options
  • Name (required) – The name attribute of the HTML element
  • ID (required) – The id of the HTML element
  • Value – The value attribute of the HTML element
  • Class – The class attribute of the HTML element
Labels
  • ID (required) – The id of the HTML element
  • For (required) – The id of the parent HTML element
  • Label (required) – The text value that will be added to the HTML element
  • Class – The class attribute of the HTML element
Example
new WPPL_Form('wppl-test-form', wp_create_nonce('wppl-test-form'), array(
    array(
        'element' => 'input',
        'type' => 'text',
        'name' => 'wppl-text-input',
        'id' => 'wppl-text-input'
    ),
    array(
        'element' => 'select',
        'options' => array(
            array(
                'name' => 'wppl-option-one',
                'id' => 'wppl-option-one',
                'value' => 'the first option'
            ),
            array(
                'name' => 'wppl-option-two',
                'id' => 'wppl-option-two',
                'value' => 'the second option'
            )
        ),
        'name' => 'wppl-select-input',
        'id' => 'wppl-select-input'
    ),
    array(
        'element' => 'label',
        'id' => 'wppl-label',
        'class' => 'wppl-label',
        'for' => 'wppl-checkbox-input',
        'label' => 'Tick this if you are awesome'
    ),
    array(
        'element' => 'input',
        'type' => 'checkbox',
        'name' => 'wppl-checkbox-input',
        'id' => 'wppl-checkbox-input'
    ),
    array(
        'element' => 'input',
        'type' => 'submit',
        'name' => 'wppl-submit',
        'id' => 'wppl-submit'
    )
));
check_nonce

A static function that will save up a bit of your time when you want to process the form information in the backend. You won’t have to check if the nonce exists and then validate its content. You can simply use this static method and it will do both of those.

Example
$tag = "huksystudios-is-awesome";

$nonce = wp_create_nonce($tag);

WPPL_Form::check_nonce($nonce, $tag);
Response
(bool) True