-
wp_nonce_url 와 wp_verify_nonce워드프레스 2023. 4. 11. 21:27
https://wp-kama.com/function/wp_nonce_url
wp_nonce_url() – Adds a nonce token to the URL: ?_wpnonce=9d6bd884a1. WP Function.
Adds a nonce token to the URL: ?_wpnonce=9d6bd884a1.
wp-kama.com
wp_nonce_url() │ WP 2.0.4
Adds a nonce token to the URL: ?_wpnonce=9d6bd884a1.
The result of the function is intended only for output, and not for internal usage in PHP (e.g. for redirect with wp_redirect()). That's because the functions escapes the value with esc_html().
Uses: wp_create_nonce()1 time — 0.000102 sec (fast) | 50000 times — 2.28 sec (fast)No Hooks.
Return
String. Escaped URL with nonce action added.
Usage
$actionurl(string) (required)URL to add nonce action.$action(int|string)Nonce action name.wp_nonce_url( $actionurl, $action, $name );
Default: -1$name(string)Nonce name.
Default: '_wpnonce'Examples
0#1 Basic example
echo wp_nonce_url( 'http://example.com/url' ); // output: http://example.com/url?_wpnonce=1ef8422137 echo wp_nonce_url( 'http://example.com/url?arg=data' ); // output: http://example.com/url?arg=data&_wpnonce=9d6bd884a1
0#2 Another basic example
echo wp_nonce_url( 'http://example.com/url', 'my_nonce' ); // output: http://example.com/url?_wpnonce=4a875e9c59 // then check the url if( wp_verify_nonce( $_GET['_wpnonce'], 'my_nonce' ) ) echo "Check passed"; else echo "Check failed";
0#3 Add nonce to the URL for use in wp_redirect()
The result of wp_nonce_url() is intended only for output on the screen, not for internal usage in PHP. If you want to add the nonce to the URL and use it in some function (e.g. wp_redirect()), you can do something like this:
$url = '/wp-admin/admin.php?page=foo'; $nonce = wp_create_nonce( 'my_nonce_key' ); // create nonce $nonce_url = add_query_arg( [ '_wpnonce'=>$nonce ], $url ); echo $nonce_url; //> /wp-admin/admin.php?page=foo&_wpnonce=74c42a878c
Add Your Own Example
Changelog
Since 2.0.4 Introduced. wp_nonce_url() code WP 6.1.1
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 ) ); }1844 1845 1846 1847
'워드프레스' 카테고리의 다른 글
Build a Custom WordPress User Flow — Part 2: New User Registration (0) 2023.04.13 Build a Custom WordPress User Flow — Part 1: Replace the Login Page (2) 2023.04.13 How to Create Custom RSS Feeds in WordPress (0) 2023.03.29 wp get category info by post id (0) 2023.03.28 [WordPress] pre_get_posts 에 대해 알아보자 (0) 2023.03.23