Home >Backend Development >PHP Tutorial >How do I Pass Extra Variables in WordPress URLs?
Passing Extra Variables in WordPress URLs
In WordPress, you may encounter difficulties passing additional variables through URLs. For instance, trying to add "&c=123" to "/news" may only work for the root URL (www.example.com?c=123) but fail if the URL contains additional information (www.example.com/news?c=123).
To address this issue, WordPress provides three essential functions:
Example:
. On the page where you create the link or set the query variable:
<a href="<?php echo esc_url(add_query_arg('c', $my_value_for_c)); ?>">
<a href="<?php echo esc_url(add_query_arg('c', $my_value_for_c, site_url('/some_other_page/'))); ?>">
. In functions.php or a plugin file:
function add_custom_query_var($vars) { $vars[] = "c"; return $vars; } add_filter('query_vars', 'add_custom_query_var');
. On the page where you want to retrieve and use the query variable:
$my_c = get_query_var('c');
On the Back End (wp-admin)
When accessing the backend (wp-admin), the main WP Query is not executed, and thus query variables are unavailable. Instead, you should use the following approach:
$my_c = filter_input(INPUT_GET, "c", FILTER_SANITIZE_STRING);
The above is the detailed content of How do I Pass Extra Variables in WordPress URLs?. For more information, please follow other related articles on the PHP Chinese website!