Is everything I need to code here?
If it works then yes!
If it does not work then no there is stuff missing.
Only you can answer this by opening the site and checking if it does what it’s supposed to do.
Is the code missing any WordPress best practices?
This is a very open ended question and cannot be answered concretely or correctly ( super subjective ).
Note that because this is a WooCommerce filter it might have other things that can’t be talked about here specific to that plugin.
There is 1 thing though: When you call wp_redirect
you have to call exit;
immediatley afterwards.
Is the code breaking any WordPress standards?
This is a very open ended question. The most charitable interpretation is, does this code pass the WPCS ruleset from the PHPCodeSniffer tool?
This is the output using the WPCS v3.1 ruleset:
❯ phpcs wpse-test.php --standard="WordPress"
FILE: wpse-test.php
------------------------------------------------------------------------------------------------------------------------------------------------------
FOUND 37 ERRORS AND 4 WARNINGS AFFECTING 22 LINES
------------------------------------------------------------------------------------------------------------------------------------------------------
1 | ERROR | [ ] There must be no blank lines before the file comment
4 | ERROR | [x] Whitespace found at end of line
12 | ERROR | [ ] There must be exactly one blank line after the file comment
12 | ERROR | [ ] Missing @package tag in file comment
13 | ERROR | [x] Whitespace found at end of line
14 | ERROR | [ ] Missing doc comment for function bhhs74_thankkyou_add_params_redirect()
16 | ERROR | [x] Tabs must be used to indent lines; spaces are not allowed
18 | ERROR | [x] Tabs must be used to indent lines; spaces are not allowed
19 | ERROR | [x] Tabs must be used to indent lines; spaces are not allowed
19 | ERROR | [ ] Assignments must be the first block of code on a line
19 | ERROR | [x] Concat operator must be surrounded by a single space
19 | ERROR | [x] String "/bhhs-declined/" does not require double quotes; use single quotes instead
20 | ERROR | [x] Tabs must be used to indent lines; spaces are not allowed
20 | WARNING | [ ] wp_redirect() found. Using wp_safe_redirect(), along with the "allowed_redirect_hosts" filter if needed, can help avoid any
| | chances of malicious redirects within code. It is also important to remember to call exit() after a redirect so that no other
| | unwanted code is executed.
21 | ERROR | [x] Tabs must be used to indent lines; spaces are not allowed
23 | ERROR | [x] Tabs must be used to indent lines; spaces are not allowed
23 | ERROR | [x] Expected 1 spaces after opening parenthesis; 0 found
23 | ERROR | [x] Expected 1 spaces before closing parenthesis; 0 found
24 | ERROR | [x] Tabs must be used to indent lines; spaces are not allowed
24 | WARNING | [x] Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space
26 | ERROR | [x] Tabs must be used to indent lines; spaces are not allowed
26 | ERROR | [ ] Assignments must be the first block of code on a line
26 | ERROR | [x] Concat operator must be surrounded by a single space
26 | ERROR | [x] String "/thank-you/" does not require double quotes; use single quotes instead
27 | ERROR | [x] Tabs must be used to indent lines; spaces are not allowed
27 | ERROR | [x] Whitespace found at end of line
28 | ERROR | [x] Tabs must be used to indent lines; spaces are not allowed
29 | ERROR | [x] Tabs must be used to indent lines; spaces are not allowed
29 | WARNING | [x] Array double arrow not aligned correctly; expected 6 space(s) between "'bhhs74_order_email'" and double arrow, but found 1.
29 | ERROR | [ ] Variable "$creditCardEmail" is not in valid snake_case format, try "$credit_card_email"
30 | ERROR | [x] Tabs must be used to indent lines; spaces are not allowed
30 | ERROR | [ ] Variable "$ticketCount" is not in valid snake_case format, try "$ticket_count"
31 | ERROR | [x] Tabs must be used to indent lines; spaces are not allowed
31 | ERROR | [x] Whitespace found at end of line
32 | ERROR | [x] Tabs must be used to indent lines; spaces are not allowed
32 | ERROR | [x] Whitespace found at end of line
33 | ERROR | [x] Tabs must be used to indent lines; spaces are not allowed
35 | ERROR | [x] Tabs must be used to indent lines; spaces are not allowed
35 | WARNING | [ ] wp_redirect() found. Using wp_safe_redirect(), along with the "allowed_redirect_hosts" filter if needed, can help avoid any
| | chances of malicious redirects within code. It is also important to remember to call exit() after a redirect so that no other
| | unwanted code is executed.
37 | ERROR | [x] Expected 1 newline at end of file; 0 found
37 | ERROR | [x] Function closing brace must go on the next line following the body; found 1 blank lines before brace
------------------------------------------------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 31 MARKED SNIFF VIOLATIONS AUTOMATICALLY
------------------------------------------------------------------------------------------------------------------------------------------------------
Time: 112ms; Memory: 10MB
Is there a better way to build the target URL with permalinks?
The way the URLs are being generated is super weird and unnecessary. Take this line:
$url = site_url( $path="", $scheme = null ) . "/thank-you/";
$path="";
is super weird, it looks like this was copy pasted from the reference doc.
This would be the cleaner equivalent:
$url = site_url( '', null ) . '/thank-you/';
and since those are the default values they can be removed:
$url = site_url() . '/thank-you/';
But even this has issues as the path is being appended separately despite there being a dedicated parameter for it, so this is the more correct version:
$url = site_url( 'thank-you' );
The same goes for rest_url
and home_url
etc..
You could try to use get_permalink
to fetch the full URL of the thankyou page but this would require you to either store the post ID of the thankyou page, or query the database to find it. I don’t think either of these are optimal.