okuley/wordpress-nonce

以面向对象的方式实现WordPress Nonces的全部功能。在根目录中包含一个单元测试用例。

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

This package is not auto-updated.

Last update: 2024-09-28 19:47:29 UTC


README

一个组合包,用于在面向对象环境中处理WordPress Nonces的功能

目录

需求

  • PHP >= 5.3.0
  • WordPress >= 4.6

安装

您可以在命令行或将其粘贴到插件目录的根目录中安装此类。

通过命令行

使用Composer,将自定义Nonce类添加到您插件的依赖项中。

composer require okuley/wordpress-nonce:dev-master

另一种方法

  1. 下载此存储库的最新zip文件master.zip
  2. 解压master.zip文件。
  3. 将其复制并粘贴到您插件目录的根目录中。
  4. 继续您的项目。

使用方法

设置最小要求的东西

<?php 

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

use Nonces\WpNonce;


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

示例

向URL添加nonce

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

向表单添加nonce

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

创建nonce

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

验证nonce

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

验证通过AJAX请求传递的nonce

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

在其他上下文中验证nonce

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