Wallogit.com
2017 © Pedro Peláez
Object oriented functionality for WP Nonce
WP Nonce Function if object oriented (OOP) way to handle with WordPress wp_nonce_*() functions., (*1)
Table of contents: * Requirements * Installation * Usage, (*2)
You can install this class both on command-line or by pasting it into the root of your plugin directory., (*3)
Using Composer, add Custom Nonce Class to your plugin's dependencies., (*4)
composer require creativform/wp_nonce_function:dev-master
Setup the minimum required thigs:, (*5)
<?php // Autoload files using Composer autoload require_once __DIR__ . '/../vendor/autoload.php'; // use class use Nonce\WP_Nonce; // Instantiate the class $nonce = new WP_Nonce();
Basic global setup for all object is nonce_name and nonce_action. You can define it once and don't use in other rendering for this session., (*6)
$nonce->option([
'nonce_name' => '_wpnonce',
'nonce_action' => 'edit-post_'.$post->ID,
]);
Also you have session time expire if you need it:, (*7)
$nonce->option( 'nonce_life',(4 * HOUR_IN_SECONDS) );
Like you see, you can pass array of settings or use single setup like above., (*8)
Adding a nonce to a URL:, (*9)
$url="/../wp-admin/post.php?post=48"; $complete_url = $nonce->url( $url );
Adding a nonce to a form:, (*10)
$nonce->field();
creating a nonce:, (*11)
$newnonce = $nonce->create();
NOTE: $nonce->create(); passing values inside other objects and you don't need to use string to pass values but optional function return value., (*12)
Verifying a nonce:, (*13)
$nonce->wp_verify();
Verifying a nonce passed in an AJAX request:, (*14)
$nonce->ajax_verify( 'post-comment' );
Verifying a nonce passed in admin area:, (*15)
$nonce->admin_verify( $_REQUEST['my_nonce'] );
Destructing and finish, (*16)
When you finish, just close this session and destruct all setups, (*17)
$nonce->clean();