PHPackages                             alin999/demo-wp-nonces-oop - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. alin999/demo-wp-nonces-oop

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

alin999/demo-wp-nonces-oop
==========================

Composer package, It implements working with WordPress Nonces in an object orientated way

08PHP

Since Apr 23Pushed 8y ago1 watchersCompare

[ Source](https://github.com/alin999/demo-wp-nonces-oop)[ Packagist](https://packagist.org/packages/alin999/demo-wp-nonces-oop)[ RSS](/packages/alin999-demo-wp-nonces-oop/feed)WikiDiscussions master Synced today

READMEChangelogDependenciesVersions (1)Used By (0)

Composer Package - WoprPress Nonces OOP Class
=============================================

[](#composer-package---woprpress-nonces-oop-class)

Composer package, which serves the functionality working with WordPress Nonces. Implementing wp\_nonce\_\*() function in an object orientated way.

Installation
------------

[](#installation)

shell:

```
	composer require alin999/demo-wp-nonces-oop
```

or, just add in your composer file:

```
{
    "repositories": [
        {
            "type": "vcs",
            "url" : "https://github.com/alin999/demo-wp-nonces-oop"
        }
    ],
    "require": {
        "alin999/demo-wp-nonces-oop" : "dev-master"
    }
}

```

Public Functions
----------------

[](#public-functions)

- [set\_action](#set_action)
- [get\_action](#get_action)
- [set\_nonce\_name](#set_nonce_name)
- [get\_nonce\_name](#get_nonce_name)
- [create\_nonce](#create_nonce)
- [nonce\_to\_url](#nonce_to_url)
- [nonce\_to\_field](#nonce_to_field)
- [nonce\_ays](#nonce_ays)
- [verify\_nonce](#verify_nonce)
- [check\_admin\_referer](#check_admin_referer)
- [check\_ajax\_referer](#check_ajax_referer)

Usage
-----

[](#usage)

```
    $my_nonce = new WP_Nonce;
```

### set\_action

[](#set_action)

*It sets a custom nonce action*
Parameters:

- **$action** (string/int) (optional) Nonce action. If missing, the daluat value will be -1

```
    $my_nonce->set_action('test-action');
    echo $my_nonce->action;
```

### get\_action

[](#get_action)

*Returns the nonce action*

```
    $action = $my_nonce->get_action();
    echo $action;
```

### set\_nonce\_name

[](#set_nonce_name)

\_It sets a custom nonce name
Parameters:

- **$name** (string/int) (optional) Action name. If missing, the default value is \_*wpnonce*

```
    $my_nonce->set_nonce_name( '_wpnonce' );
    echo $my_nonce->nouce_name;
```

### get\_nonce\_name

[](#get_nonce_name)

*Returns the nonce name*

```
    $name = $my_nonce->get_nonce_name();
    echo $name;
```

### create\_nonce

[](#create_nonce)

*Function to generate and return a nonce based on WordPress [wp\_create\_nonce](https://codex.wordpress.org/Function_Reference/wp_create_nonce) function*
Returns: string
Parameters:

- **$action** (string/int) (optional) Nonce action.Optional. If missing, the class var ($my\_nonce-&gt;action) will be used

```
    $nonce = $my_nonce->create_nonce();
    echo $nonce;
```

### nonce\_to\_url

[](#nonce_to_url)

*Function to add a nonce to an URL and return the updated URL. It wraps WordPress function [wp\_nonce\_url](https://codex.wordpress.org/Function_Reference/wp_nonce_url)*
Returns: string : the URL with nonce action added
Parameters:

- **$actionurl** string URL where to add nonce action
- **$action** string|int Optional. Nonce action name. If null or blank, the class var (action) will be used
- **$name** string Optional. Nonce name. If missing, null or blank, the class var (nonce\_name) will be used

```
    $test_url = $my_nonce->nonce_to_url( 'http://my-wp-site.com', -1, '_wpnonce');
    echo $test_url;
```

### nonce\_to\_field

[](#nonce_to_field)

*Function to retrieve or display the nonce hidden form field. It wraps WordPress function [wp\_nonce\_field](https://codex.wordpress.org/Function_Reference/wp_nonce_url)*
Returns: string : the nonce hidden form field
Parameters:

- **$action** string|int Optional. Nonce action name. If null or blank, the class var (action) will be used
- **$name** string Optional. Nonce name. If missing, null or blank, the class var (nonce\_name) will be used
- **$referer** boolean Optional. Whether also the referer hidden form field should be created with the wp\_referer\_field() function. Default is true
- **$echo** boolean Optional. Whether to display or return the nonce hidden form field. Defalut is true

```
    $nonce_field = $my_nonce->nonce_to_field( -1, '_wpnonce', true, false );
    echo $nonce_field;
```

### nonce\_ays

[](#nonce_ays)

*Function to display 'Are you sure you want to do this?' message to confirm the action being taken.It wraps WordPress function [wp\_nonce\_ays](https://codex.wordpress.org/Function_Reference/wp_nonce_ays)*
Parameters:

- **$action** string|int Optional. Nonce action name. If null or blank, the class var (action) will be used

```
    $my_nonce->nonce_ays();
```

### verify\_nonce

[](#verify_nonce)

*Function to verify that a nonce is correct and unexpired with the respect to a specified action.It wraps WordPress function [wp\_verify\_nonce](https://codex.wordpress.org/Function_Reference/wp_verify_nonce)*
Returns: (boolean|integer) False if the nonce is invalid. Otherwise, returns an integer with the value of: *1* – if generated in the past 12 hours or less or *2* – if generated between 12 and 24 hours ago.
Parameters:

- **$nonce** string Required. Nonce to verify.
- **$action** string|int Optional. Nonce action name. If null or blank, the class var (action) will be used

```
    $nonce = $my_nonce->create_nonce();
    $verify_nonce_response = $my_nonce->verify_nonce($nonce);
    echo $verify_nonce_response;
```

### check\_admin\_referer

[](#check_admin_referer)

*Function to tests either if the current request carries a valid nonce, or if the current request was referred from an administration screen; depending on whether the $action argument is given (which is prefered), or not, respectively. On failure, the function dies after calling the wp\_nonce\_ays() function.*
*It wraps WordPress function [check\_admin\_referer](https://codex.wordpress.org/Function_Reference/check_admin_referer)*
Returns: To return boolean true, in the case of the obsolete usage, the current request must be referred from an administration screen; in the case of the prefered usage, the nonce must be sent and valid. Otherwise the function dies with an appropriate message ("Are you sure you want to do this?" by default). Parameters:

- **$action** string|int Optional. Nonce action name. If null or blank, the class var (action) will be used
- **$query\_arg\_name** string Optional. Nonce name. Where to look for nonce in the $REQUEST PHP variable. If missing, null or blank, the class var (nonce\_name) will be used

```
	//You add a nonce to a form using the wp_nonce_field() function:

	//Then in the page where the form submits to, you can verify whether or not the form was submitted and update values if it was successfully submitted:
