alexlg89/wpnonce

Wordpress wp_nonce* 函数的包装类。

安装次数: 23

依赖项: 0

建议者: 0

安全: 0

星标: 0

关注者: 2

分支: 1

开放问题: 0

类型:composer-wordpress-package

0.0.1 2017-03-31 10:59 UTC

This package is not auto-updated.

Last update: 2024-09-20 18:54:12 UTC


README

WpNonce 是一个静态包装类,用于包装 Wordpress wp_nonce* 函数。

安装

composer require alexlg89/wpnonce

或者只需添加

"require alexlg89/wpnonce": "0.0.1"

到您的 composer.json 文件中,并运行 composer 更新。

使用方法

创建一个包含 nonce 参数的 URL

$url = 'http://mysite.com/custommers';
$action = 'add-customer';
$name = '_myNonce';
$nonceUrl = WpNonce::url($url, $action, $name);

或者只需通过跳过最后一个参数使用默认名称。

$nonceUrl = WpNonce::url($url, $action);

创建一个具有特定操作的 nonce 字段

$action = 'add-customer';
WpNonce::field($action);

您还可以将第二个参数设置为 referer。

$referer = 'http://mysite.com/dashboard';
WpNonce::field($action, $referer);

第三个参数允许您仅获取 nonce 字段并跳过 referer 字段,如果设置为 false。

WpNonce::field($action, $referer, false);

如果您将第四个参数设置为 false,则字段函数可以返回字符串形式的 html。

$html = WpNonce::field($action, $referer, true, false);

创建一个具有特定操作的 nonce

$action = 'add-customer';
$nonce = WpNonce::create($action);

检查 URL 是否包含有效的 nonce

$action = 'add-customer';
$name = '_myNonce';
$retval = WpNonce::checkAdminReferer($action, $name);

或者只需通过跳过最后一个参数使用默认名称。

$retval = WpNonce::checkAdminReferer($action);

检查 AJAX URL 是否包含有效的 nonce

$action = 'add-customer';
$queryArg = '_myNonce';
$retval = WpNonce::check_ajax_referer($action, $queryArg);

如果第三个参数设置为 false,则脚本在 nonce 无效时不会崩溃。

$retval = WpNonce::check_ajax_referer($action, $queryArg, false);

使用特定操作验证 nonce

$nonce = 'an2bf72h';
$action = 'add-customer';
$retval = WpNonce::verify($nonce, $action);

默认 nonce

const DEFAULT_NONCE = '_wpnonce';