函数文档

wp_nonce_url()

💡 云策文档标注

概述

wp_nonce_url() 函数用于在 URL 查询字符串中添加 nonce(一次性令牌),以增强安全性,防止 CSRF 攻击。它接受一个 URL 和可选参数,返回经过转义的 URL。

关键要点

  • 函数签名:wp_nonce_url( $actionurl, $action = -1, $name = '_wpnonce' )
  • 参数:$actionurl(必需,要添加 nonce 的 URL),$action(可选,nonce 操作名称,默认 -1),$name(可选,nonce 名称,默认 '_wpnonce')
  • 返回值:转义后的 URL,包含 nonce 查询参数
  • 内部实现:使用 wp_create_nonce() 生成 nonce,add_query_arg() 添加到 URL,esc_html() 进行转义
  • 注意事项:函数会将 URL 中的 & 转义为 &,可能影响链接或重定向的正确性,需注意使用场景

代码示例

// 示例 URL,注意其中的 &
$url = 'http://localhost/?arg1=value1&arg2=value2';

// 输出:http://localhost/?arg1=value1&arg2=value2&_wpnonce=abcdef
echo wp_nonce_url( $url, 'action' );

// 对比:使用 add_query_arg 直接添加 nonce,不转义 &
echo add_query_arg( '_wpnonce', wp_create_nonce( 'action' ), $url );

注意事项

  • wp_nonce_url() 会自动转义 & 为 &,这可能导致某些链接或重定向行为异常,建议在需要原始 URL 时考虑替代方案。
  • 在插件开发中,常结合 admin_url() 使用,并通过 wp_verify_nonce() 验证 nonce 有效性以确保安全。
  • 如果页面有多个可操作按钮,应为每个按钮设置不同的 nonce 名称或上下文,以避免混淆。

📄 原文内容

Retrieves URL with nonce added to URL query.

Parameters

$actionurlstringrequired
URL to add nonce action.
$actionint|stringoptional
Nonce action name.

Default:-1

$namestringoptional
Nonce name. Default '_wpnonce'.

Return

string Escaped URL with nonce action added.

Source

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 ) );
}

Changelog

Version Description
2.0.4 Introduced.

User Contributed Notes

  1. Skip to note 3 content

    Note that wp_nonce_url escapes & to & and may cause links or redirects to become incorrect.

    // Sample URL, note the & in there
    $url = 'http://localhost/?arg1=value1&arg2;=value2';
    
    // This will show <a href="http://localhost/?arg1=value1&amp;arg2=value2&amp;_wpnonce=abcdef" rel="nofollow ugc">http://localhost/?arg1=value1&amp;arg2=value2&amp;_wpnonce=abcdef</a>
    echo wp_nonce_url( $url, 'action' );
    
    // This will return <a href="http://localhost/?arg1=value1&arg2=value2&_wpnonce=abcdef" rel="nofollow ugc">http://localhost/?arg1=value1&arg2;=value2&_wpnonce=abcdef</a>
    echo add_query_arg( '_wpnonce', wp_create_nonce( 'action' ), $url );

  2. Skip to note 4 content

    Example

    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:

    function my_plugin_do_something () {
    ?>
    

    <a href="" class="button button-primary">

    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.”

    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')) {
    ?>
    

    <a href="" class="button button-primary">

    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.