When add_query_arg() is necessary?

The difference is, in “plugin_part1-fixed.php” you wouldn’t lose existing args. Consider the url:

https://www.example.com/page/?foo=bar

Your code in “plugin_part1.php” would output

<a href="https://www.example.com/page/?custom_var=column">text</a>

Note, the existing arg “foo” has been lost, the code in “plugin_part1-fixed.php” would output:

<a href="https://www.example.com/page/?foo=bar&custom_var=column">text</a>

Note, the existing arg “foo” is still present and your arg has been appended.

Since you don’t have any existing args you don’t see a difference. That said, using add_query_arg() is still best practice since you don’t know if you’ll have to interact with another plugin that might also be sending args for whatever reason.

Also, please keep in mind that you should always wrap add_query_arg() in esc_url() to prevent XSS scripting as better explained here.

Leave a Comment