media-store-net/wp-oop-nonces

A simple Way to handle nonces in your plugin or theme on a OOP environment

dev-master 2019-12-04 21:55 UTC

This package is auto-updated.

Last update: 2024-03-05 07:27:12 UTC


README

Description

By developing WordPress Plugins and Themes sometimes you need store your own Project-Settings into the wp_options table. This Tool make it easier to handle the wp_nonce_* functions in a OOP-environment.

Installation

Easiest way to install is using composer,

composer require media-store-net/wp-oop-nonces

or clone this Repo with

git clone https://github.com/media-store-net/wpnonces.git

How to use

In general, you can use this class without further adjustment.

As an example I have created a plugin, where i will use this package to handle nonce.

Step 1 : require the autoload.php to make it accessible in your plugin/theme | can be emited by using composer

  • download the Repo and store it in vendor folder in your plugin
require_once WP_PLUGIN_DIR . '/' . plugin_basename( __DIR__ ) . '/vendor/WpNonces/vendor/autoload.php';

Step 2 : load an instance in your settings or options page

$wp_nonces = \MediaStoreNet\WpNonces\WpNonces::getInstance();
this static method allows you to use allways the same instance of the class
in all your settings/options files.

Step 3: create a form In my case i do this on a separate function and call these in my settings/options page

<?php function my_form( $wp_nonces ) { ob_start(); ?>

<form method="get" action="options.php">
    <label for="testinput">Input</label>
    <input type="text" id="testinput" name="testinput"/>
	<?php $wp_nonces->getNonceField(); ?>
	<?php submit_button( 'speichern' ); ?>
</form>

<?php return ob_get_clean(); ?>

Step 4: Validate

to validate you have several options

call $wp_nonce->verify('nonceString')

if ( $wp_nonces->verifyNonce($_REQUEST['_wpnonce']) ): //store options...; endif;

call $wp_nonce->verifyAdmin()

if ( $wp_nonces->verifyAdmin() ): // store options...; endif;

call $wp_nonces->verifyAjax() to validate Ajax Requests

if ( $wp_nonces->verifyAjax() ): //store ajax request options...; endif;

By default is used fieldName of nonce "_wpnonce" like in WordPress use. The action string is "wp-oop-nonce"

For more secure and customized nonce you can modify the fieldName and Action string too

$wp_nonces->setFieldName('my-custom-name'); $wp_nonces->setAction('my-custom-action');

When needed you can also use more then one instance like this

$nonces1 = new MediaStoreNet\WpNonces\WpNonces(); $nonces1->setFieldName('name1'); $nonces1->setAction('action1');

$nonces2 = new MediaStoreNet\WpNonces\WpNonces(); $nonces2->setFieldName('name2'); $nonces2->setAction('action2');

To see all available propertys and methods, please visit our API Documentation Site

Minimum Requirements / Dependencies

  • PHP ^7.0
  • WordPress latest-2

When installed for development, via Composer requires:

  • phpunit/phpunit (BSD-3-Clause)
  • brain/monkey (MIT)
  • inpsyde/php-coding-standards

Documentation

Please visit our Documentation Site API Documentation Site

CHANGELOG

Link to changelog

Licence and Copyright

GPLv2+ Licence

Copyright (c) 2019 Media-Store.net