Minimum Plugin En

Lingua Italiana

An example of a plugin for WordPress.

Minimum Plugin is a small plugin, useful for WordPress novices who plan to write a plugin or customize their site.

My wish is that this article will allow you to save some of the time needed to familiarize yourself with WordPress..

MinimumPlugin is composed of a single file ( MinimumPlugin.php ).

It contains an administrative page with a form for 3 input fields.

There is a shortcode [Minimum Plugin] to be inserted in a frontend page.

The amount of code is kept to the bare minimum.

What does Minimum Plugin do? Well, it display a Countdown on the frontend page.

The WordPress rules require that the main file MinimumPlugin.php starts with a description, the header.

References:
Basic Header

/*
 Plugin Name: Minimum Plugin
 Plugin URI: https://wpd.edilweb.eu
 Description: This is an example plugin for beginners. It is very small, it displays a countdown.
 Version: 1.0.0
 Author: wpd.edilweb.eu
 Author URI: https://wpd.edilweb.eu
 License: GPL-2
 License URI: http://www.gnu.org/licenses/gpl-2.0.txt
 Text Domain: MinimumPlugin
 Domain Path: /languages
 */

A precaution is added to prevent the file from being called from outside WordPress

if ( !defined( 'ABSPATH' )) { exit; }

A activate function to activate Minimum Plugin.
References:
Activate

function activate_MinimumPlugin() {}

A deactivate function to disable Minimum Plugin.
References:
Deactivate

function deactivate_MinimumPlugin(){}

A function to call TextDomain which in our case is the same as the name of the plugin. The function is necessary to localize the plugin.
References:
load text domain

function textDomain_MinimumPlugin() {
     global $pP;
     load_plugin_textdomain( $pP->plugin_name );
 }
donna in piedi

We register in WordPress a function that points to our function of validation of the administrative form, register_setting_MinimumPlugin()
References:
Register Setting

function register_setting_MinimumPlugin() {
      global $pP;
      register_setting( $pP->plugin_name, $pP->plugin_name,
'validate_settings_MinimumPlugin' );
  }

Now the links to the administrative page must be set.
Like other functions we write, they also need a WordPress “hook”. In the Main Code section of the file after the function declarations, there are calls to WordPress “hooks”.
The next two functions bring up the settings link of the plugin in the “Installed plugins” and the MinimumPlugin link in “Settings”.
References:
Options page
Hooks

function add_filter_link_MinimumPlugin( $links ) {
    $mylinks = array( '<a href="' 
    . admin_url( 'options-general.php?page=' 
    . plugin_basename(_FILE_) ) . '">Settings>/a' );

    return array_merge( $links, $mylinks );
}

function add_options_page_MinimumPlugin(){
     global $pP;
     $page_title = $pP->plugin_name;
     $menu_title = $pP->plugin_name;
     $capability = 'manage_options';
     $menu_slug = plugin_basename(_FILE_); 
     if( $pP->cheForm === 'simple' ){
         $function = 'display_setup_MinimumPlugin';
     }
     elseif ( $pP->cheForm === 'complex' ) {
          $function = 'display_setup2_MinimumPlugin';
     }    
    add_options_page( $page_title, $menu_title, $capability, $menu_slug, $function );
 }

After the declaration, the administrative page is made available for WordPress with the display_setup2_MinimumPlugin () function.
Inside there is all the administrative HTML, but first you need to access the data stored in the database concerning this plugin, in the #_options table.
We do this using the WordPress function get_option ($ var) .
The Administrative Options of the plugin are stored in this table ( #_options ) by default, and the options is already present in the database at this point of execution of the script.
The initial options of Minimum Plugin are only set once, in the Main Code section of this file.
Table and database references:
Database


The URL of the form is still one of the WordPress services, the admin_url() function
References:
admin url


It is necessary to add some control and security fields to the form.
WordPress settings_fields( $arg ); function.
References:
Nonce


The input fields of the Minimum Plugin administrative form, which in this case sends an array with three values to the server, are similar to this:

<td>
  <input 
    type="text" 
    name="<?php echo $plugin_option; ?>[mpTxt]" 
<!-- name_of_the_option[key_option] --> 
    id="mp_txt" 
    title="Countdown event" 
    value="<?php echo $db_options['mpTxt']; ?>" 
<!-- value of the option from database -->
    required>
</td>

The submit button of the form is obtained from the WordPress function submit_button ();

Function display_setup2_MinimumPlugin()
function display_setup2_MinimumPlugin(){
     global $pP;
     $url = admin_url( 'options.php' );
     $db_options = get_option( $pP->plugin_name );
 /*  translations    */
$cEvent = __( 'Countdown event', 'MinimumPlugin' ); 
$cDate = __( 'Countdown departure date', 'MinimumPlugin' ); 
$cDini = __( 'Countdown start', 'MinimumPlugin' );
$cTime = __( 'Countdown start time', 'MinimumPlugin' );
$cTini = __( 'Time start', 'MinimumPlugin' );

$toBw = <<<EOF
<div class="wrap">
<h1>Minimum Plugin Settings</h1>
<form action="{$url}" method="post" 
id="MinimumPlugin-form">
EOF;
     echo $toBw;

     settings_fields( $pP->plugin_name );

$toBw2 = <<<BW2
<table class="form-table">
<tbody>
<tr>
<th scope="row">
<label for="mp_txt">Your Event </label>
</th>
<td>
<input type="text" name="{$pP->plugin_name}[mpTxt]" 
id="mp_txt" title="{$cEvent}" 
value="{$db_options['mpTxt']}" required>
</td>
</tr>
<tr>
<th scope="row">
<label for="mp_Data">{$cDate} </label>
</th>
<td>
<input type="date" name="{$pP->plugin_name}[mpData]" 
id="mp_Data"  title="{$cDini}" 
value="{$db_options['mpData']}" required>
</td>
</tr>
<tr>
<th scope="row">
<label for="mp_Time">{$cTime} </label>
</th>
<td>
<input type="time" name="{$pP->plugin_name}[mpTime]" 
id="mp_Time"  title="{$cTini}" 
value="{$db_options['mpTime']}" 
required>
</td>
</tr>
</tbody>
</table>
BW2;
echo $toBw2; 

submit_button();

$toBw3 = <<<BW3
     </form>
 </div>
 BW3;
 echo $toBw3;
}

 

The form’s inputs are validated before they are registered in the database. Function validate_settings_MinimumPlugin ( $input )

function validate_settings_MinimumPlugin( $input ){
     /*  here the code to validate form     */    
     global $pP; 
if( $pP->cheForm === 'simple' ){ /*  single option  1 field */ 
  $valid = sanitize_text_field( $input );
}
elseif ( $pP->cheForm === 'complex' ) { /*  3 fields    */     
 $valid = []; 
$valid[ 'mpTxt' ] = sanitize_text_field( $input[ 'mpTxt' ]);
$valid[ 'mpData' ] = sanitize_text_field( $input[ 'mpData' ]);
$valid[ 'mpTime' ] = sanitize_text_field( $input[ 'mpTime' ]);
} 
else {
die('error');
}
return $valid;
}

Once the administrative functions are over, we record in WordPress a function that signals the possible presence of a shortcode , shortcodes_MinimumPlugin_init() .
References:
Shortcode

function shortcodes_MinimumPlugin_init(){
     global $pP;
     add_shortcode($pP->shortcode,
'shortcodes_MinimumPlugin_exec');
 }

We add a function for executing the shortcode in the front-end. shortcodes_MinimumPlugin_exec()

Function shortcodes_MinimumPlugin_exec()
function shortcodes_MinimumPlugin_exec(){
 global $pP;
 $db_options = get_option( $pP->plugin_name );
   switch ($pP->cheForm) {
    case 'complex':   /* Here $db_options is an array */
$a = <<<EOF
<p style="font-size: 20px; margin-bottom: 0;">{$db_options['mpTxt']}<br>{$db_options['mpData']}   time {$db_options['mpTime']}<br><br>Time to end:</p>
<p style="height: 50px; font-size: 30px; margin-top: 0px;" id="idMinPlug"></p>
<script>
var dateFromDb = '{$db_options['mpData']}', timeFromDb = '{$db_options['mpTime']}';var countDownDate = new Date( dateFromDb + ' ' + timeFromDb ).getTime();
var xMinimumPlugin = setInterval(function () {var now = new Date().getTime();var distance = countDownDate - now;if (distance > 0) {var days = Math.floor(distance / (1000 * 60 * 60 * 24));var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));var seconds = Math.floor((distance % (1000 * 60)) / 1000);idMinPlug.innerHTML = days + "d - " + hours + "h - " + minutes + "m : " + seconds + "s ";} else {clearInterval(xMinimumPlugin);idMinPlug.innerHTML = "EXPIRED";}}, 1000);
</script>
EOF;
   break;
   case 'simple':  /*  Here $db_options is a simple variable   */
$a = <<<EOF
<p style="font-size: 20px; margin-bottom: 0;">Countdown start: <br>day {$db_options}   <br><span style="font-size: 13px;">Time to end:</span></p>
<p style="height: 50px; font-size: 30px; margin-top: 0px;" id="idMinPlug"></p>
<script>
var dateFromDb = '{$db_options}', timeFromDb = '00:00';var countDownDate = new Date( dateFromDb + ' ' + timeFromDb ).getTime();
var xMinimumPlugin = setInterval(function () {var now = new Date().getTime();var distance = countDownDate - now;if (distance > 0) {var days = Math.floor(distance / (1000 * 60 * 60 * 24));var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));var seconds = Math.floor((distance % (1000 * 60)) / 1000);idMinPlug.innerHTML = days + "d - " + hours + "h - " + minutes + "m : " + seconds + "s ";} else {clearInterval(xMinimumPlugin);idMinPlug.innerHTML = "EXPIRED";}}, 1000);
</script>
EOF;
    break;

   default:
     $a = 'error "shortcodes_MinimumPlugin_exec"';
     break;
    }
    
    return $a;
}

 

Composizione_spaziale_Katarzyna_Kobro

At this point we have to execute the functions declared before.
Activation and deactivation of the plugin:
References:
activation hook

$pP = new stdClass;
$pP->plugin_name = 'MinimumPlugin';
$pP->version = '1.0.0';
//  $pP->cheForm = 'simple';        /*  simple = form admin with one field */
$pP->cheForm = 'complex';           /*  complex = form admin with more than one field    */
$pP->shortcode = 'MinimumPlugin';
register_activation_hook( __FILE__, 'activate_MinimumPlugin' );
register_deactivation_hook( __FILE__, 'deactivate_MinimumPlugin' );


Hooks the functions on to a specific action.
References:
Action Reference
add action/

add_action( 'plugins_loaded', 'textDomain_MinimumPlugin' );
add_action('init', 'shortcodes_MinimumPlugin_init');
add_action( 'admin_init', 'register_setting_MinimumPlugin' );
add_action( 'admin_menu', 'add_options_page_MinimumPlugin' ); 

Hooks a functions on to a specific WordPress filter.
References:
action links

add_filter( 'plugin_action_links_'.
 plugin_basename(__FILE__),
 'add_filter_link_MinimumPlugin' ); 

Finally, there is the inclusion in the database of the initial administrative options. This block is executed only once.

if( false == get_option( $pP->plugin_name )){
    if( $pP->cheForm === 'simple' ){  /*  single option  1 field */
        add_option($pP->plugin_name, '2020-12-25' );
    }
    elseif ( $pP->cheForm === 'complex' ) { /*  3 fields    */
        $ppIniVal = [ 'mpTxt' => 'Wedding Anniversary', 
                'mpData' => '2020-12-25', 'mpTime' => '00:00' ];
        add_option( $pP->plugin_name, ( array )$ppIniVal );
    }
    else { die('error'); }
} 

Minimum Plugin Example

Pasqua - Easter
2023-04-09  time 00:01

Waiting Time:

Test Ajax FrontEnd



Download the plugin. It is installed and activated like the others.

DownLoad MinimumPlugin 2.1.0