Languages: English • 日本語 (Add your language)
Retrieve URL with nonce added to URL query.
The returned result is escaped for display.
<?php wp_nonce_url( $actionurl, $action, $name ); ?>
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 () { ?> <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."
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.
Since: 2.0.4
wp_nonce_url() is located in wp-includes/functions.php
.
Nonce functions: wp_nonce_ays(), wp_nonce_field(), wp_nonce_url(), wp_verify_nonce(), wp_create_nonce(), check_admin_referer(), check_ajax_referer(), wp_referer_field()