Explain the method of adding query string to link in WordPress _php tips

Source: Internet
Author: User

The query string refers to the query after the question mark in the link, formatted as Key=value, and multiple query statements separated by & symbols. The Add_query_arg () function is a handy function provided by WordPress to help you add one or more query statements to a link.

Some people will ask, add the query string directly behind the link manually appended to it? This is sometimes possible, but if the link itself has a query string, it can cause a malformed problem. And also is not conducive to PHP writing management, and the Add_query_arg () function to help you think about everything, you only need to give the query string to add a link and one or more key values and key names can be.

Usage

Add_query_arg ($key, $value, $old _query_or_uri);
Add_query_arg ($query, $old _query_or_uri);

Parameters

Add_query_arg () has two uses, the first of which requires three parameters.

$key

(Integer | String) (must) the key value of the parameter to be added, like an array, if the string is a name, and the number selects the nth argument.

Default value: None

$value

(mixed) (must) the value to be added.

Default value: None

$old _query_or_uri

(string) (optional) The link that is processed.

Default value: False, write false to $_server[' Request_uri ']

The second usage requires two parameters, using an array to pass the query string to be added to the link.

$query

(array) (must) the query to be added, the key value and the key name correspond respectively.

Default value: None

$old _query_or_uri

(string) (optional) The link that is processed.

Default value: False, write false to $_server[' Request_uri ']

return value

(string) A link to add a good query string.

Example

First usage:

echo add_query_arg (' id ', 123, ' http://www.jb51.net ');

Second usage:

echo add_query_arg (Array (' ID ' => 123), ' http://www.jb51.net ');

Two of the results are http://www.jb51.net?id=123.

Other special cases:

echo add_query_arg (Array (' ID ' => 123), ' Http://www.jb51.net?query=post ');//http://www.jb51.net?query=post& Id=123
Echo add_query_arg (array (' ID ' => 123, ' cat ' => ' themes '), ' http://www.jb51.net?query=post ');//http:/ /www.jb51.net?query=post&id=123&cat=themes
Echo add_query_arg (array (' ID ' => 123, ' cat ' => ' themes ') , ' query ' => ' page '), ' http://www.jb51.net?query=post ');//http://www.jb51.net?query=page&id=123&cat= Themes

Example

This function is located at: wp-includes/functions.php

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.