wp_nonce_url()获取在查询中添加了随机数的URL

目录

描述

译文

检索在查询中添加了随机数的URL。

原文

Retrieve URL with nonce added to URL query.

The returned result is escaped for display.

wp_nonce_url() 描述

用法

<?php wp_nonce_url$actionurl$action$name ); ?>

wp_nonce_url() 用法

参数

$actionurl

(string) (必填) URL to add nonce action

默认值: None

$action

(string) (可选) nonce action name

默认值: -1

$name

(string) (可选) nonce name

默认值: _wpnonce

wp_nonce_url() 参数

返回值

(string) 

URL with nonce action added.

wp_nonce_url() 返回值

示例

Plugin authors can safely add links that perform tasks using a combination of wp_nonce_url() and admin_url().

For instance, start by creating the link users can click to do something interesting:

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
function my_plugin_do_something () {
?>
<h2><?php esc_html_e('My Plugin Admin Screen', 'my-plugin-textdomain');?></h2>
<p>
    <a href="<?php print wp_nonce_url(admin_url('options.php?page=my_plugin_settings'), 'doing_something', 'my_nonce');?>"
        class="button button-primary"><?php esc_html_e('Do Something!', 'my-plugin-textdomain');?></a>
    <span class="description"><?php esc_html_e('This button does something interesting.', 'my-plugin-textdomain');?></span>
</p>
<?php
}

Then, to detect when the user clicks the link, check the nonce validity using wp_verify_nonce() in the function you defined when you called add_menu_page() or one of its Administration Menus wrappers. If the nonce isn't valid, the link wasn't clicked, so display the link. Otherwise, do "something interesting."

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
add_action('admin_menu', 'add_my_plugin_admin_screen');
function add_my_plugin_admin_screen () {
    add_options_page(
        __('My Plugin Settings', 'my-plugin-textdomain'),
        __('My Plugin', 'my-plugin-textdomain'),
        'manage_options',
        'my_plugin_settings',
        'my_plugin_do_something'
    );
}

function my_plugin_do_something () {
    if (!isset($_GET['my_nonce']) || !wp_verify_nonce($_GET['my_nonce'], 'doing_something')) {
?>
<h2><?php esc_html_e('My Plugin Admin Screen', 'my-plugin-textdomain');?></h2>
<p>
    <a href="<?php print wp_nonce_url(admin_url('options.php?page=my_plugin_settings'), 'doing_something', 'my_nonce');?>"
        class="button button-primary"><?php esc_html_e('Do Something!', 'my-plugin-textdomain');?></a>
    <span class="description"><?php esc_html_e('This button does something interesting.', 'my-plugin-textdomain');?></span>
</p>
<?php
    } else {
        // User pressed "Do Something!" button, so
        // do something interesting.
    }
}

Note that the recommended "context" parameter of the nonce is used to disambiguate which button was pressed. If you make more than one button users can press, make sure each button has a different nonce name and/or context.

wp_nonce_url() 示例

历史

添加于 版本: 2.0.4

wp_nonce_url() 历史

源文件

wp_nonce_url() 函数的代码位于 wp-includes/functions.php.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Retrieve URL with nonce added to URL query.
 *
 * @since 2.0.4
 *
 * @param string     $actionurl URL to add nonce action.
 * @param int|string $action    Optional. Nonce action name. Default -1.
 * @param string     $name      Optional. Nonce name. Default '_wpnonce'.
 * @return string Escaped URL with nonce action added.
 */
function wp_nonce_url( $actionurl, $action = -1, $name = '_wpnonce' ) {
	$actionurl = str_replace( '&', '&', $actionurl );
	return esc_html( add_query_arg( $name, wp_create_nonce( $action ), $actionurl ) );
}

wp_nonce_url() 源文件

相关