okuley/wordpress-nonce

Implementation of the whole featured WordPress Nonces in an object oriented way. Included in the root directory is a unit test case.

dev-master 2016-09-16 09:14 UTC

This package is not auto-updated.

Last update: 2025-01-18 21:36:13 UTC


README

A compose package which serves the functionality of working with WordPress Nonces in object oriented environment

Table of contents:

Requirements

  • PHP >= 5.3.0
  • WordPress >= 4.6

Installation

You can install this class both on command-line or by pasting it into the root of your plugin directory.

via Command-line

Using Composer, add Custom Nonce Class to your plugin's dependencies.

composer require okuley/wordpress-nonce:dev-master

Another way

  1. Download the latest zip of this repo.
  2. Unzip the master.zip file.
  3. Copy and paste it into the root of your plugin directory.
  4. Continue with your project.

Usage

Setup the minimum required thigs:

<?php 

require_once __DIR__ . '/../vendor/autoload.php'; // Autoload files using Composer autoload

use Nonces\WpNonce;


// Instantiate an object of the class
$nonce = new WpNonce();

Examples

Adding a nonce to a URL:

$url="/../wp-admin/post.php?post=48";
$complete_url = $nonce->wp_nonce_url( $url, 'edit-post_'.$post->ID );

Adding a nonce to a form:

$nonce->get_wp_nonce_field( 'delete-post_'.$post_id );

creating a nonce:

$newnonce = $nonce->wp_create_nonce( 'action_'.$post->id );

Verifying a nonce:

$nonce->wp_verify_nonce_field( 'delete-post_'.$post_id );

Verifying a nonce passed in an AJAX request:

$nonce->wp_check_ajax_referer( 'post-comment' );

Verifying a nonce passed in some other context:

$nonce->wp_check_admin_referer( $_REQUEST['my_nonce'], 'edit-post_'.$post->ID );