Home > Backend Development > PHP Tutorial > How to Pass Extra Variables in URLs with WordPress?

How to Pass Extra Variables in URLs with WordPress?

Susan Sarandon
Release: 2024-11-11 14:17:03
Original
1001 people have browsed it

How to Pass Extra Variables in URLs with WordPress?

Passing Extra Variables in URLs with WordPress

WordPress allows for the passing of extra variables in URLs, providing a convenient way to store and access information. However, adding extra variables to URLs can sometimes encounter difficulties.

Original Issue:

As highlighted in the inquiry, a user faced the challenge of passing an extra variable named 'c' in the URL of a WordPress installation. This variable was recognized at the website root (www.example.com?c=123) but failed to work in URLs containing additional information (www.example.com/news?c=123).

Solution:

The suggested solution involves utilizing the following three WordPress functions:

  • add_query_arg(): Creates the URL with the desired query variable.
  • query_vars filter: Modifies the list of known public query variables (front-end only).
  • get_query_var(): Retrieves the value of a custom query variable passed in the URL.

Implementation:

On the Page Creating the Link/Query Variable:

  • Use add_query_arg() to append the query variable to the URL.

In functions.php or a Plugin File (Front-End Only):

  • Add a custom query variable to the list of recognized ones using the query_vars filter.

On the Page Retrieving and Utilizing the Query Variable:

  • Use get_query_var() to retrieve the value of the custom query variable.

Additional Considerations for the Back End (wp-admin):

As the WP Query is not employed in wp-admin, the recommended approach is to examine the $_GET superglobal using filter_input() or the traditional isset() method.

Conclusion:

By implementing the functions and filter described above, you can effectively pass extra variables in URLs within your WordPress installation. This allows for greater flexibility in storing and accessing information, enhancing the functionality and user experience of your website.

The above is the detailed content of How to Pass Extra Variables in URLs with WordPress?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template